ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   shell function + wait parameter (https://www.excelbanter.com/excel-programming/431238-shell-function-wait-parameter.html)

pls123

shell function + wait parameter
 
hi all i found this page..

http://msdn.microsoft.com/en-us/libr...yk(VS.80).aspx

i need the wait parameter to be activated for my shell ...

x = Shell("C:\abc.exe")

can anybody show me the right syntax if the case is possible..?

tx all

Rick Rothstein

shell function + wait parameter
 
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).

--
Rick (MVP - Excel)


"pls123" wrote in message
...
hi all i found this page..

http://msdn.microsoft.com/en-us/libr...yk(VS.80).aspx

i need the wait parameter to be activated for my shell ...

x = Shell("C:\abc.exe")

can anybody show me the right syntax if the case is possible..?

tx all



pls123

shell function + wait parameter
 
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 ?




"Rick Rothstein" wrote:

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).

--
Rick (MVP - Excel)


"pls123" wrote in message
...
hi all i found this page..

http://msdn.microsoft.com/en-us/libr...yk(VS.80).aspx

i need the wait parameter to be activated for my shell ...

x = Shell("C:\abc.exe")

can anybody show me the right syntax if the case is possible..?

tx all





All times are GMT +1. The time now is 01:50 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com