Hi Richard, In the process of finding a solution for my problem described below I found on the internet your videos about Access VBA. I want you to compliment with the quality of the videos and specially about the way you teach a subject. It is always right to the point without unnecessary side paths. I am not one of your students so I hesitated a bit to ask you the question below. In the past I did wrote several Access VBA applications and I am writing a new one at the moment. For that application I need a functionality to start a web browser from without the VBA application and Suspend (not pause) that VBA code until the web browser is terminated. As a solution I found a Microsoft article "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 into my application 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)
' 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 word 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)? In the meantime, I learned that Firefox is a fundamental other "thing" than Word or Notepad? Do you have a suggestion for a solution for the above wished functionality? Regards Wim Bruyn
Kevin Yip
@Reply 3 years ago
The Devhut website has a procedure that seems to work (I just tried). It's a relatively short VB sub that uses WMI technique (Windows Management Instrumentation):
Just copy the code to a standard module in Access. I ran the following test and it worked -- the code correctly waited for Firefox to exit:
Sub WMITest()
Shell "C:\Program Files\Mozilla Firefox\firefox.exe"
Do Until Not WMI_IsProcessRunning("firefox.exe")
Loop
MsgBox "Process ended."
End Sub
In Devhut's code, notice that it actually runs a query that checks if a process is running or not:
"SELECT * FROM Win32_Process WHERE Name = '" & sProcessName & "'"
Regarding the original code you used, I've used it for years -- since the 1990s, so it goes way back. It works with lots of programs, like VLC, Adobe Reader, etc., but it also doesn't work with many, like Firefox, Thunderbird, etc.
Sorry, only students may add comments.
Click here for more
information on how you can set up an account.
If you are a Visitor, go ahead and post your reply as a
new comment, and we'll move it here for you
once it's approved. Be sure to use the same name and email address.
This thread is now CLOSED. If you wish to comment, start a NEW discussion in
Visitor Forum.