WD97: Shell Command Does Not Wait for Application to Finish

Last reviewed: August 27, 1997
Article ID: Q171127
The information in this article applies to:
  • Microsoft Word 97 for Windows

SUMMARY

When you use the Shell command to launch another program from a Visual Basic for Applications macro, Word does not wait for the launched program to finish before running the rest of the macro. This article contains a sample macro that can determine if a launched program is still running.

MORE INFORMATION

Visual Basic for Applications processes macro commands asynchronously. This means that each command runs independently of any timing process, such as a clock.

Because macros do not wait for the launched program to finish before running the next command, this can cause problems, particularly if subsequent commands rely on results from the launched program.

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

This Visual Basic for Applications macro sample uses a combination of a Do Loop with the Exists method to determine if the launched program is still running.

   Sub IsTaskRunning()
      ' If file does not exist, GoTo error trap.
      On Error GoTo itrErrTrap
      ' Run sample batch file in MS-DOS window.
      Shell "c:\test.bat"
      Do
         ' Note: When specifying the Application name
         ' with the Exists(Name) argument, omit the
         ' Application extension.
         test = Tasks.Exists("test")
      Loop Until  test = 0
      MsgBox "The application has quit."
   itrErrTrap: ' Note: This line must be left aligned.
      If Err > 0 then MsgBox Err.Description
   End Sub

REFERENCES

For more information about the Exists Method, switch to the Visual Basic Editor, click the Office Assistant, type "Exists" (without the quotation marks), click Search, and then click to view the "Exists Method" topic.

For more information about the shell command, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE ID: Q79696
   TITLE     : Using Shell Statement to Access MS-DOS Prompt from WinWord

   ARTICLE ID: Q72287
   TITLE     : Using AppActivate to Switch to Another Application


Additional query words: 8.0 8.0 vba vbe vb
Keywords : kbcode kbinterop kbmacro kbprg kbmacroexample word8 word97
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 27, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.