XL: How to Force Macro Code to Wait for Outside Procedure

ID: Q147392


The information in this article applies to:
  • Microsoft Excel 97 for Windows
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel for Windows, version 5.0


SUMMARY

In the versions of Microsoft Excel listed at the beginning of this article, you can use a Visual Basic for Applications macro to run other Windows and MS-DOS applications and procedures. The macro code in Microsoft Excel continues to execute even after the external procedure has been initiated. You must write code to handle delays if Microsoft Excel is to wait for the output from the outside procedure.


MORE INFORMATION

Microsoft provides programming examples 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. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp

Creating the Module

Before you use the examples in this article, follow these steps:
  1. Open a new workbook.


  2. Insert a new module sheet. To do this in Microsoft Excel 97, point to Macro on the Tools menu, and click Visual Basic Editor. In the Visual Basic Editor, click Module on the Insert menu.

    In Microsoft Excel 7.0, point to Macro on the Insert menu and click Module.


  3. Type the example macro code in the module sheet.


Example 1: Creating a Flag

For the macro code in example 1 to work correctly, you need to make your custom application create a flag when it has completed execution. In the following example your custom application should create a text file at C:\Flag.txt to act as this flag.

   Sub Appacttest()

      ' Checks to see if Flag.txt already exists.
      FindIt = Dir("C:\Flag.txt")

      ' If the file Flag.txt has been found then delete it.
      If  Not Len(FindIt) = 0  Then
         Kill "C:\Flag.txt"
      End If

      ' Sets Myapp variable equal to the Shell statement.
      Myapp = Shell("C:\Custom.exe", 1)

      ' Executes the shell statement.
      AppActivate Myapp

      ' Checks to see if Flag.txt can be found yet.
      FindIt = Dir("C:\Flag.txt")

   ' The following While Wend loop will keep Microsoft Excel "suspended"
   ' until the custom application is complete. This will occur while the
   ' length of the FindIt variable is equal to 0. Microsoft Excel will
   ' remain busy until it finds the file Flag.txt, thus making the length
   ' of FindIt > 0 and ending the loop.

      ' Check to see if the length of FindIt variable is equal to 0
      ' chars.
      While Len(FindIt) = 0

      ' Continue to check if flag was created yet.
      FindIt = Dir("C:\Flag.txt")

      Wend

      ' Continue with more code if needed.

   End Sub 

Example 2: Using an Intermediate File to Avoid a Sharing Violation

This example uses an intermediate file to allow the MS-DOS DIR command to complete and close the output before Microsoft Excel attempts to open it. If this method were not used, the Workbooks.Open method would generate a sharing violation by attempting to open the output while it was still being written.

While the example below illustrates one use of this procedure, you could apply the same method to any MS-DOS or Windows application that generates an output file that can be read by Microsoft Excel.

   Sub WaitForOutput()

   If Len(Dir("c:\output.txt")) > 0 Then Kill "c:\output.txt"
   If Len(Dir("c:\temp.txt")) > 0 Then Kill "c:\temp.txt"
   ' Test for previous files and delete them.

   Shell "command.com /c dir c:\windows\*.* > c:\temp.txt"
   ' Run MS-DOS DIR command to pipe the directory of
   ' c:\windows into an intermediate text file, temp.txt.

   On Error Resume Next
   ' Set error condition to skip to the next line,
   ' for Name statement below.

   Do Until Len(Dir("c:\output.txt")) > 0
   ' Begin a loop to test for final output file, output.txt.

      Name "c:\temp.txt" As "c:\output.txt"
      ' Attempt to rename temp.txt to output.txt;
      ' will fail until temp.txt is closed

      DoEvents
      ' Allow for other processes, including the shelled
 ' procedure above, to continue in the background Loop
   Loop
   'End the loop

   Workbooks.Open ("c:\output.txt")
   ' Open the resulting text file, output.txt, into an Excel worksheet.

   End Sub 


REFERENCES

For additional information, please see the following articles in the Microsoft Knowledge Base:

Q129796 How to Determine When a Shelled 32-bit Process Has Terminated

Q96844 How to Determine When a Shelled 16-bit Process Has Terminated
For more information about Shell function in Microsoft Excel 97, click the Index tab in Visual Basic for Applications Help, type the following text


   Shell 


and then double-click the selected text to go to the "Shell function" topic.

For more information about the Shell function in Microsoft Excel 7.0, click Answer Wizard on the Help menu and type:
tell me about the Shell function

Additional query words: 5.0 5.0c 5.00c 8.00 97 xl97 pause

Keywords : kbcode kbprg
Version : WINDOWS:5.0,7.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: October 30, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.