Option Explicit
Public Sub Run_Word()
Dim x1 As Object
Dim x2 As Object
Set x1 = CreateObject("Word.Application")
Set x2 = x1.Documents.Open("C:\vidu.doc")
x1.Visible = True
End Sub
Bạn hiểu rằng mỗi tập tin đều do một chương trình quản lý nhất định (*.doc là sản phẩm của MS Word, *.xls là sản phẩm của MS Excel). Điều quan trọng là bạn phải biết executable programe nào có thể khởi động được các tập tin này. Dễ thôi, hãy nghĩ tới Task Manager - trình quản lý bộ nhớ và các chương trình đang thi hành. Trong thẻ Processes bạn sẽ biết được EXE nào bạn vừa kích hoạt. Đây chính là mấu chốt để bạn sử dụng hàm Shell:Runs an executable program and returns a Variant (Double) representing the program's task ID if successful, otherwise it returns zero.
Syntax
Shell(pathname[,windowstyle])
The Shell function syntax has these named arguments:
pathname: Required; Variant (String). Name of the program to execute and any required arguments or command-line switches; may include directory or folder and drive. On the Macintosh, you can use the MacID function to specify an application's signature instead of its name. The following example uses the signature for Microsoft Word:
Shell MacID("MSWD")
windowstyle: Optional. Variant (Integer) corresponding to the style of the window in which the program is to be run. If windowstyle is omitted, the program is started minimized with focus. On the Macintosh (System 7.0 or later), windowstyle only determines whether or not the application gets the focus when it is run.
Dim strApplication as String, strFullname As String, objVidu As Object
strApplication= "WinWord"
strFullname = "C:\Vidu.doc"
Shell strApplication & " " & strFullname
Set objVidu = ActiveWindow
[COLOR=darkgreen]'Bạn lưu ý sau khi làm việc xong với objVidu thì cần Set objVidu = Nothing để giải phóng bộ nhớ.[/COLOR]
Dim strApplication as String, strFullname As String, objhh As Object
strApplication= "hh"
strFullname = "C:\PROGRA~1\COMMON~1\MICROS~1\VBA\VBA6\1033\VbLR6.chm::/html/vafctShell.htm"
Shell strApplication & " " & strFullname
Set objhh = ActiveWindow