Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

vba - How To Programmatically Wait For Shell Command To Finish Running?

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).

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...