I am writing a VBA application and need a functionality to start a web browser from without the VBA application and suspend that VBA code until the web browser is terminated.
As a solution i found an Microsoft artikel "Determine when a shelled process ends". (https://learn.microsoft.com/en-us/office/vba/access/concepts/windows-api/determine-when-a-shelled-process-ends).
I copied the code and it all works fine for programs like Notepad, Exel, Word etc. Part of the code:
Private Sub Wait_for_shell_Click()
ExecCmd "NOTEPAD.EXE": MsgBox "Process 1 Finished"
ExecCmd "C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE": MsgBox "Process 2 Finished"
ExecCmd "C:\Program Files\Mozilla Firefox\firefox.exe": MsgBox "Process 3 Finished"
ExecCmd "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe": MsgBox "Process 4 Finished"
End Sub
However, the VBA code is not suspended for both the web browsers Firefox and Edge but suspends for Notepad and Word.
The ExecCmd routine looks as followed:
Public Sub ExecCmd(cmdline As String)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ReturnValue As Integer
' Initialize the STARTUPINFO structure:
start.cb = Len(start)
' Start the shelled application:
ReturnValue = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
' Wait for the shelled application to finish:
Do
ReturnValue = WaitForSingleObject(proc.hProcess, 0)
DoEvents
Loop Until ReturnValue <> 258
'Loop Until ReturnValue <> 1
ReturnValue = CloseHandle(proc.hProcess)
End Sub
When debugging the code i noticed that for Notepad and wordt the ReturnValue in the Do loop stays on 258 until Notepad or Word terminates.
However for firefox or Edge the ReturnValue is not 258, so the Do loop terminates immediately
I have no knowledge c.q. experience about the Windows API so my question is can i use the above method for the functionality i am looking for (I get the feeling not)?
Is Firefox a fundamental other "thing" than Word or Notepad?
I debugged the VBA code, looking for more explanation about WaitForSingleObject function.
This is not a complete answer, but since there are no other posts:
The return value explanation may give you enough information to pursue this further.