vba 如何以编程方式等待 Shell 命令完成运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5968356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:12:56  来源:igfitidea点击:

How To Programmatically Wait For Shell Command To Finish Running?

shellvbasmtp

提问by sooprise

I am sending emails in a program like so:

我在这样的程序中发送电子邮件:

Call Shell(smtpPath, emailInput...)

This works great, except if I call the function twice, the function runs the first time, calls the shell command, then the function runs again, and calls the shell command again, but the first shell command is not completed, so there is an error because the second shell command is trying to use the same smtp file as the first one (that is still in use).

这很好用,除非我调用函数两次,函数第一次运行,调用shell命令,然后函数再次运行,再次调用shell命令,但是第一个shell命令没有完成,所以有一个错误,因为第二个 shell 命令试图使用与第一个相同的 smtp 文件(仍在使用中)。

How can I make the function wait until the shell command has finished running?

如何让函数等待 shell 命令完成运行?

Addendum: Or is there a way I can see if the file is being used, and if it is, sleep it, then try again?

附录:或者有什么方法可以查看文件是否正在被使用,如果是,则休眠它,然后再试一次?

采纳答案by Anders Lindahl

You could use WScript.Shell, and it's runmethod:

您可以使用WScript.Shell,它是run方法:

Set objShell = CreateObject("WScript.Shell")
result = objShell.Run(smtpPath & " " & emailInput, 0, true)