Reputation: 73
Well, I'm sure it's out there but I can't find it..
I want to shell out to a program (from Access VBA) and then wait for it to complete. There are examples of this using Shell but it seems like with Shell you have to pass the executable and not just the file path/name. Since this will be used on multiple machines, I can't guarantee what the executable will be on the target machine (eg which pdf viewer is set up) so I want to use the defaults.
I can use Shellexecute to start a file with the default program, but I can't get the window handle to run the wait routines. Since I need to run multiple external pieces, I need to wait for each one to complete before I launch the next one.
So. Can anyone help me with an idea of how to get Shell to use the default program or how to get the handle for a Shellexecute window or suggest another API that will do all of the pieces I need?
Thanks - Alan
Upvotes: 1
Views: 290
Reputation: 29342
The following routine opens a file according to the association in the registry, in blocking mode:
Sub OpenFileAndWait(fullName As String)
CreateObject("WScript.Shell").Run """" & fullName & """", 3, True
End Sub
ps.1: param 3
opens the document in maximized window, and the last param True
specifies waiting mode. Enclosing the file's name with double-quotes eliminates the trouble with names that include "space" for the command interpreter.
ps.2:
There have been lots of discussions about how to achieve this, and the proposed methods take you to complicated APIs such as ShellExecuteEx
, CreateProcess
, WaitForSingleObject
etc, each time with a different set of parameters and deficiencies. None of those methods was complete and satisfactory: some API's (Shell
or CreateProcess
) require to specify the executable application, while others (ShellExecute
or ShellExecuteEx
) can use file association but alas, they are non-blocking and it is very complicated to get the process handle in order to use WaitForSingleObject
).
Upvotes: 4