I understand how to use ShellExecute in VBA (for my Outlook macros) but I\'m looking to be able to use ShellExecuteEx to wait for the executed program in my script. Does an
CreateProcess()
Windows API call instead.For running a process and waiting until it finishes, use solution recommended by Microsoft which calls CreateProcessA()
. Do not use ShellExecuteEx()
. (You can also consider replacing your existing code.)
Reasons:
It is recommended directly by manufacturer. There can be several reasons behind it including recent one:
It is stable. ShellExecuteEx() is reported to throw exception after recent (2015) Windows updates when it is called from VBA.
In the answer to the above linked question, the code from Microsoft article is ready-to-use as separate VBA module.
Old question but here's a much simpler answer:
Sub ShellAndWait(pathFile As String)
With CreateObject("WScript.Shell")
.Run pathFile, 1, True
End With
End Sub
(You could even squish it to one line, but this is easier to read.)
Sub demo_Wait()
ShellAndWait ("notepad.exe")
Beep 'this won't run until Notepad window is closed
MsgBox "Done!"
End Sub
Adapted from (and more options at) Chip Pearson's site.
If I am not mistaken, you need to set the SEE_MASK_NOASYNC bit in the fMask parameter.
Const SEE_MASK_NOASYNC As Long = &h0&
With Prop
.fMask = &HC Or SEE_MASK_NOASYNC
End With
Edit
Hmmm, I'm having trouble getting it to work too. Have you considered just GetFileInformationByHandle?