|
应该是等待时间过长. 两种方法
1.Shell("命令", vbHide, True) 最后参数为true 这个可选参数设为True就可以让Shell执行完才接着Shell下边的代码
2.使用API来判断 建立以下的代码
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("F:\1.bat") ' 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 "执行完毕 "
End Sub
|
|