按下暂停后如何在这种情况下调用第二个shell?
Shell "cmd /c cd %tmp% && echo hello > tmpfile && pause", 1
Shell "cmd /c cd %tmp% && echo hello > tmpfile && pause", 1
在这种情况下
Shell "cmd /c cd %tmp% && echo hello > tmpfile", 0
Shell "cmd /c cd %tmp% && echo hello > tmpfile", 0
您正在使用的Shell
命令是asynchronous,这意味着它可以一次运行多个进程,因此在连续两个Shell
语句的情况下,两者将同时执行。
另一种方法是使用Windows脚本宿主(Run
)的WScript.Shell
命令,因为它有更多选项,包括等待程序执行在继续之前完成的选项。
Sub ShellWait(fName As String, Optional showWindow As Boolean = True)
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
wsh.Run fName, -showWindow, True
End Sub
您还可以通过将False
指定为第二个参数来完全隐藏窗口。 (如果需要用户输入,请注意该选项!)
例如:
Sub demo()
ShellWait "x:\test.bat"
Beep
MsgBox "Finished running!"
End Sub