shell function + wait parameter

R

Rick Rothstein

There is no "wait parameter" for the Shell command; however, you can make
your code wait using some simple Windows API calls. Follow these
instructions to see how...

Go to the VBA editor and add a Module (Insert/Module from the menu bar) and
then copy/paste these lines in its (General)(Declarations) section:

Public Declare Function OpenProcess _
Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Public Declare Function CloseHandle _
Lib "kernel32" _
(ByVal hObject As Long) As Long

Public Declare Function WaitForSingleObject _
Lib "kernel32" _
(ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long


Now, in your own code, call your Shell command in this format with the
appropriate Shell arguments placed in the parentheses:

PID = Shell( <<Put Shell Arguments Here>> )

Next, paste the following IMMEDIATELY after the PID=Shell statement above
(making sure to handle the possible error where indicated; i.e. stop the
code from falling through to your other commands if the Shell failed):

If PID = 0 Then
'
'Handle Error, Shell Didn't Work
'
Else
hProcess = OpenProcess(&H100000, True, PID)
WaitForSingleObject hProcess, -1
CloseHandle hProcess
End If

And finally, Dim both the PID and hProcess variables as Long. Now, when you
run your code, it will wait for the Shell command to finish (if you are
actually opening the Command Window, your code will wait for you to Exit
from it).
 
P

pls123

hi rick ty for your help !

can it use timeout, for being sure that excel will not hang if there is any
error from the launched program..??

where should i indicate ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top