Option Explicit '等待shell执行完成后才执行下一条代码 Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Const PROCESS_QUERY_INFORMATION = &H400 Private Const STATUS_PENDING = &H103 Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = &HFFFFFFFF Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
'执行Shell命令并等待程序执行完毕 再返回控制权才继续执行下一句代码 Private Sub Command1_Click()
Dim pId As Long, pHnd As Long ' 分别声明 Process Id 及 Process Handle 变数 pId = Shell("http://www.office-cn.net") ' Shell 传回 Process Id
pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process Handle DoEvents ' doevents If pHnd <> 0 Then Call WaitForSingleObject(pHnd, INFINITE) ' 无限等待,直到程序结束 Call CloseHandle(pHnd) End If MsgBox "执行完毕 "