vba:完成上一个shell后执行shell

问题描述 投票:0回答:1

按下暂停后如何在这种情况下调用第二个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
vba
1个回答
1
投票

您正在使用的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

更多信息:

© www.soinside.com 2019 - 2024. All rights reserved.