OFF97: How to Show a "Now Processing" Dialog While Macro Runs

ID: Q162257

The information in this article applies to:

SUMMARY

This article describes how you can display a dialog box while a macro is running in the background. The purpose of displaying a dialog box is to alert the user that the application is busy while your macro is running.

MORE INFORMATION

When you use the Show method to display a UserForm, the UserForm is shown "modal"; that is, the user must respond to the UserForm before using any other part of the application. After a UserForm is loaded, no subsequent code is executed until the UserForm is either hidden or unloaded.

To display a dialog box and allow the macro to run in the background, you can run your macro in the Activate event of the UserForm. When the UserForm is shown, the Activate event is triggered and your macro will run while the UserForm is shown on the screen. The following steps illustrate how you can accomplish this:

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.

 1. Start a new workbook in Microsoft Excel 97 (or a new document in
    Microsoft Word 97).

 2. Press ALT+F11 to activate the Visual Basic Editor.

 3. On the Insert menu, click Module to insert a module into the project.

 4. Press F4 to display the Properties window.

 5. Change the Name property of the module to "Processing_Code" (without
    the quotation marks).

 6. In the Processing_Code module window, type the following:

      Public Processing_Message As String
      Public Macro_to_Process As String

      Sub StartProcessing (msg As String, code As String)

         Processing_Message = msg    'Set the message that is displayed
                                     'in the dialog box

         Macro_to_Process = code     'Set the macro that is run after the
                                     'dialog box is active

         Processing_Dialog.Show      'Show the Dialog box

      End Sub

 7. On the Insert menu, click UserForm to add a UserForm to the project.

 8. Press F4 to display the Properties Window. Change the UserForm
    properties to the following settings.

       Property           Setting
       -------------------------------------

       Name               Processing_Dialog
       Caption            (Leave Blank)
       StartUpPosition    2-CenterScreen

 9. Add one Label control to the UserForm. Change the Name property of the
    label to "lblMessage."

10. Select the UserForm. On the View menu, click Code. In the code window,
    in the Procedure list, select the Initialize event, and then type the
    following in the Code window:

       Private Sub UserForm_Initialize()

          lblMessage.Caption = Processing_Message  'Change the Label
                                                   'Caption

       End Sub

11. In the Code window, click Activate event in the Procedure list,
    and type the following:

       Private Sub UserForm_Activate()

          Me.Repaint                         'Refresh the UserForm
          Application.Run Macro_to_Process   'Run the macro
          Unload Me                          'Unload the UserForm

       End Sub

12. On the Insert menu, click Module. Type the following code in the
    Code window:

       Sub MyMacro()

          For x = 1 to 5000
             Application.StatusBar = x   '5000 Iterations Changing
                                         'StatusBar
          Next

          Application.StatusBar = False  'Reset the StatusBar

       End Sub

       Sub Main()

          'Call the StartProcessing procedure to show the Processing_Dialog
          'with the label "Processing, Please Wait..." and execute
          'MyMacro.

          StartProcessing "Processing, Please Wait...", "MyMacro"

       End Sub

13. Press ALT+Q to leave the Visual Basic Editor and return to Microsoft
    Excel (or Microsoft Word).

14. On the Tools menu, point to Macro, and click Macros. Select the Main
    macro and click Run. The Processing_Dialog dialog box appears.

While the dialog box is shown, the status bar text in the application increments from 1 to 5000--the changing status bar text is the indication that your macro is running while the dialog box is on the screen.

How to Use the Processing Dialog Box in Other Workbooks

To use this "Processing, Please Wait" dialog box with macros in other workbooks or documents, do the following:

1. Activate the Visual Basic Editor. On the View menu, click Project

   Explorer.

2. Select the project that you created in the previous steps.

3. Right-click the "Processing_Dialog" UserForm and click Export File.

   Save the UserForm as "Processing_Dialog.frm."

4. Right-click the "Processing_Code" Module and click Export File. Save
   the module as "Processing_Code.bas."

5. Return to Microsoft Excel and open the workbook in which you would like
   to use the "Processing, Please Wait" dialog box.

6. Activate the Visual Basic Editor.

7. On the File menu, click Import File, select the "Processing_Dialog.frm"

   file, and click Open.

8. On the File menu, click Import File. Select the "Processing_Dialog.bas"
   file, and click Open.

9. On the Insert menu, click Module, and type the following code in the
   Code window:

      Sub Main()
         StartProcessing "<message text>", "<macro name>"
      End Sub

   Where the <message text> argument is the text string that you want to
   display in the Processing_Dialog dialog box and <macro name> is the
   macro that you would like to run after the dialog box appears. To show
   the dialog box and run the macro indicated by "<macro name>", run the
   macro Main.

For information on how to show a temporary message box while a macro is running in Microsoft Excel versions 5.x or 7.x, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q148209
   TITLE     : XL: How to Create a Temporary Message Box While Macro Runs

Additional query words: OffInterop xlvbahowto xlvbainfo 8.00 97 execute executing background off97 xl97 word97 splash screen
Keywords          : kbcode kbprg 
Version           : WINDOWS:97
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: April 5, 1999