WD: Shell Command Doesn't Wait for Application to Finish |
Q102019
When you use the Shell command to run another program from a WordBasic macro, Word does not wait for the shelled program to finish running before it processes the rest of the macro.
WordBasic macro processing is considered "asynchronous." This means that
macro commands are executed independently of any timing process, such as
a clock. Macros do not wait for a shelled program to finish before
executing the next command. This can cause problems in your macro,
particularly if the subsequent commands rely on processing performed
by the shelled program.
WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT
YOUR OWN RISK. Microsoft provides this macro code "as is" without
warranty of any kind, either express or implied, including but not
limited to the implied warranties of merchantability and/or fitness
for a particular purpose.
Sub MAIN
test = AppIsRunning("app string")
While test = - 1
test = AppIsRunning("app string")
Wend
MsgBox "app has quit"
End Sub
Declare Function WinExec Lib "kernel"(lpszCmdLine$, fuCmdShow As \
Integer) As Integer
Declare Sub Yield Lib "kernel"()
Declare Function GetModuleUsage Lib "kernel"(hInst As Integer) As \
Integer
Sub MAIN
WaitShell("MYPROG.EXE")
MsgBox "Done."
End Sub
Sub WaitShell(szAppToRun$)
hInst = WinExec(szAppToRun$, 1)
While GetModuleUsage(hInst) > 0
Yield 'Waiting
Wend
End Sub
Declare Function GetModuleUsage Lib "Kernel"(hModule As Integer) \
As Integer
Declare Function WinExec Lib "kernel"(lpszCmdLine$, fuCmdShow As \
Integer) As Integer
Declare Sub WaitMessage Lib "User"()
Sub MAIN
WaitShell("MYPROG.EXE") 'MYPROG.EXE is any program to run
MsgBox "Done."
End Sub
Sub WaitShell(szAppToRun$)
hMod = WinExec(szAppToRun$, 1)
If(hMod > 32) Then
While(GetModuleUsage(hMod))
WaitMessage()
Wend
Else
MsgBox "Unable to start the Application"
End If
End Sub
"Microsoft Windows Programmer's Reference," volume 2: Functions, pages
404, 979, and 983
Kbcategory: kbusage kbmacro
Additional query words: 2.0 2.0a-cd winword 7.0 word95 word7 word6 winword2 yield doevents pause
Keywords : kbmacro kbmacroexample
Issue type :
Technology :
|
Last Reviewed: November 4, 2000 © 2001 Microsoft Corporation. All rights reserved. Terms of Use. |