Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
以下代码是打开一个文档(或可执行程序)并等待这个文档关闭的例子。Public Function ShowProcess(FileName As String, ByVal OwnerhWnd As Long, Optional ByVal strCmd As String = "open") As Long Dim SEI As SHELLEXECUTEINFO Dim r As Long With SEI 'Set the structure's size .cbSize = Len(SEI) 'Set the mask .fMask = SEE_MASK_FLAG_DDEWAIT Or SEE_MASK_NOCLOSEPROCESS 'Set the owner window .hwnd = OwnerhWnd 'Show the properties .lpVerb = strCmd '"open" '"properties" 'Set the filename .lpFile = FileName .lpParameters = vbNullChar .lpDirectory = vbNullChar .nShow = SW_SHOWMAXIMIZED .hInstApp = 0 .lpIDList = 0 End With r = ShellExecuteEx(SEI) If r Then ShowProcess = SEI.hProcess End If End FunctionPublic Function WaitModifyTaskDocument(ByRef strTFn As String) As Boolean On Error GoTo ErrDeal Dim lngHProcess As Long, lngReturn As Long Dim blnR As Boolean
lngHProcess = ShowProcess(strTFn, 0) If lngHProcess <> 0 Then lngReturn = WaitForSingleObject(lngHProcess, INFINITE) If lngReturn <> csWAIT_OBJECT_0 Then SetLastErrorEx "µÈ´ýÓû§ÔĶÁ" & csTASKNOTEMESSAGE & "´íÎó£¡" GoTo ErrDeal End If blnR = CBool(TerminateProcess(lngHProcess, 0))
'John 2002-03-29 'in windows98 ev the terminateprocess function return true value 'but in winnt or window2000 ev the terminateprocess function return false value 'so set the blnr = true blnR = True End If
WaitModifyTaskDocument = blnR
ErrDeal: Select Case Err.Number Case 0 Case Else SetLastErrorEx Err.Description End Select End Function
要包括路径,后面的详细参数查msdn吧
shell "notepad.exe",vbnormal 'vbnormal 指打开方式,是最大化还是隐藏等等,可以看帮助
Dim SEI As SHELLEXECUTEINFO
Dim r As Long
With SEI
'Set the structure's size
.cbSize = Len(SEI)
'Set the mask
.fMask = SEE_MASK_FLAG_DDEWAIT Or SEE_MASK_NOCLOSEPROCESS
'Set the owner window
.hwnd = OwnerhWnd
'Show the properties
.lpVerb = strCmd '"open" '"properties"
'Set the filename
.lpFile = FileName
.lpParameters = vbNullChar
.lpDirectory = vbNullChar
.nShow = SW_SHOWMAXIMIZED
.hInstApp = 0
.lpIDList = 0
End With
r = ShellExecuteEx(SEI)
If r Then
ShowProcess = SEI.hProcess
End If
End FunctionPublic Function WaitModifyTaskDocument(ByRef strTFn As String) As Boolean
On Error GoTo ErrDeal
Dim lngHProcess As Long, lngReturn As Long
Dim blnR As Boolean
lngHProcess = ShowProcess(strTFn, 0)
If lngHProcess <> 0 Then
lngReturn = WaitForSingleObject(lngHProcess, INFINITE)
If lngReturn <> csWAIT_OBJECT_0 Then
SetLastErrorEx "µÈ´ýÓû§ÔĶÁ" & csTASKNOTEMESSAGE & "´íÎó£¡"
GoTo ErrDeal
End If
blnR = CBool(TerminateProcess(lngHProcess, 0))
'John 2002-03-29
'in windows98 ev the terminateprocess function return true value
'but in winnt or window2000 ev the terminateprocess function return false value
'so set the blnr = true
blnR = True
End If
WaitModifyTaskDocument = blnR
ErrDeal:
Select Case Err.Number
Case 0
Case Else
SetLastErrorEx Err.Description
End Select
End Function