Microsoft KB Archive/102019: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
Line 30: Line 30:
== MORE INFORMATION ==
== MORE INFORMATION ==


WordBasic macro processing is considered &quot;asynchronous.&quot; 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.<br />
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.<br />
<br />
<br />
WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code &quot;as is&quot; 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.
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.


=== Word versions 7.0, 7.0a ===
=== Word versions 7.0, 7.0a ===
Line 39: Line 39:


<pre class="CODESAMP">  Sub MAIN
<pre class="CODESAMP">  Sub MAIN
       test = AppIsRunning(&quot;app string&quot;)
       test = AppIsRunning("app string")
         While test = - 1
         While test = - 1
             test = AppIsRunning(&quot;app string&quot;)
             test = AppIsRunning("app string")
         Wend
         Wend
       MsgBox &quot;app has quit&quot;
       MsgBox "app has quit"
   End Sub </pre>
   End Sub </pre>
=== Word versions 2.0, 6.0 ===
=== Word versions 2.0, 6.0 ===
Line 49: Line 49:
The following WordBasic user-defined function, WaitShell(), uses the Windows function GetModuleUsage() to determine if the shelled application has terminated. If the WinExec() function is successful in starting the program, it returns a module handle which identifies the instance of the loaded application. When the application is no longer running, the module handle will be invalid and GetModuleUsage() will return a value of 0. WaitShell() loops until the module handle is invalid, at which point the remaining macro commands are executed.
The following WordBasic user-defined function, WaitShell(), uses the Windows function GetModuleUsage() to determine if the shelled application has terminated. If the WinExec() function is successful in starting the program, it returns a module handle which identifies the instance of the loaded application. When the application is no longer running, the module handle will be invalid and GetModuleUsage() will return a value of 0. WaitShell() loops until the module handle is invalid, at which point the remaining macro commands are executed.


<pre class="CODESAMP">  Declare Function WinExec Lib &quot;kernel&quot;(lpszCmdLine$, fuCmdShow As \  
<pre class="CODESAMP">  Declare Function WinExec Lib "kernel"(lpszCmdLine$, fuCmdShow As \  
   Integer) As Integer
   Integer) As Integer
   Declare Sub Yield Lib &quot;kernel&quot;()
   Declare Sub Yield Lib "kernel"()
   Declare Function GetModuleUsage Lib &quot;kernel&quot;(hInst As Integer) As \  
   Declare Function GetModuleUsage Lib "kernel"(hInst As Integer) As \  
   Integer
   Integer


   Sub MAIN
   Sub MAIN
       WaitShell(&quot;MYPROG.EXE&quot;)
       WaitShell("MYPROG.EXE")
       MsgBox &quot;Done.&quot;
       MsgBox "Done."
   End Sub
   End Sub


Line 67: Line 67:
   End Sub
   End Sub


   Declare Function GetModuleUsage Lib &quot;Kernel&quot;(hModule As Integer) \  
   Declare Function GetModuleUsage Lib "Kernel"(hModule As Integer) \  
   As Integer
   As Integer
   Declare Function WinExec Lib &quot;kernel&quot;(lpszCmdLine$, fuCmdShow As \  
   Declare Function WinExec Lib "kernel"(lpszCmdLine$, fuCmdShow As \  
   Integer) As Integer
   Integer) As Integer
   Declare Sub WaitMessage Lib &quot;User&quot;()
   Declare Sub WaitMessage Lib "User"()


   Sub MAIN
   Sub MAIN
       WaitShell(&quot;MYPROG.EXE&quot;)  'MYPROG.EXE is any program to run
       WaitShell("MYPROG.EXE")  'MYPROG.EXE is any program to run
       MsgBox &quot;Done.&quot;
       MsgBox "Done."
   End Sub
   End Sub


Line 85: Line 85:
         Wend
         Wend
       Else
       Else
         MsgBox &quot;Unable to start the Application&quot;
         MsgBox "Unable to start the Application"
       End If
       End If
   End Sub </pre>
   End Sub </pre>
Line 93: Line 93:
== REFERENCES ==
== REFERENCES ==


&quot;Microsoft Windows Programmer's Reference,&quot; volume 2: Functions, pages 404, 979, and 983<br />
"Microsoft Windows Programmer's Reference," volume 2: Functions, pages 404, 979, and 983<br />
<br />
<br />
Kbcategory: kbusage kbmacro
Kbcategory: kbusage kbmacro

Revision as of 09:20, 20 July 2020

WD: Shell Command Doesn't Wait for Application to Finish

Q102019



The information in this article applies to:


  • Microsoft Word for Windows, versions 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows, versions 7.0, 7.0a





SUMMARY

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.



MORE INFORMATION

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.

Word versions 7.0, 7.0a

When you use Word 7.x with Windows 95 or Windows NT, the following macro will test to see if an application is still running and won't continue on until the test application has finished running:

   Sub MAIN
      test = AppIsRunning("app string")
         While test = - 1
            test = AppIsRunning("app string")
         Wend
      MsgBox "app has quit"
   End Sub 

Word versions 2.0, 6.0

The following WordBasic user-defined function, WaitShell(), uses the Windows function GetModuleUsage() to determine if the shelled application has terminated. If the WinExec() function is successful in starting the program, it returns a module handle which identifies the instance of the loaded application. When the application is no longer running, the module handle will be invalid and GetModuleUsage() will return a value of 0. WaitShell() loops until the module handle is invalid, at which point the remaining macro commands are executed.

   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 



REFERENCES

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