Microsoft KB Archive/171127

From BetaArchive Wiki
Knowledge Base


WD97: Shell Command Does Not Wait for Application to Finish

Article ID: 171127

Article Last Modified on 1/20/2007



APPLIES TO

  • Microsoft Word 97 Standard Edition



This article was previously published under Q171127

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 professionals 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, 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:

79696 Using Shell Statement to Access MS-DOS Prompt from WinWord


72287 Using AppActivate to Switch to Another Application



Additional query words: wordcon 8.0 vba vbe vb

Keywords: kbcode kbhowto kbinterop kbmacro kbmacroexample kbprogramming KB171127