Microsoft KB Archive/162257

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

Article ID: 162257

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Office 97 Standard Edition
 * Microsoft Excel 97 Standard Edition
 * Microsoft Word 97 Standard Edition

-



This article was previously published under Q162257



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.

 Start a new workbook in Microsoft Excel 97 (or a new document in Microsoft Word 97). Press ALT+F11 to activate the Visual Basic Editor. On the Insert menu, click Module to insert a module into the project. Press F4 to display the Properties window. Change the Name property of the module to Processing_Code .  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  On the Insert menu, click UserForm to add a UserForm to the project.</li>  Press F4 to display the Properties Window. Change the UserForm properties to the following settings. <pre class="fixed_text">      Property           Setting -

Name              Processing_Dialog Caption           (Leave Blank) StartUpPosition   2-CenterScreen </li> Add one Label control to the UserForm. Change the Name property of the label to lblMessage. </li>  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 </li>  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 </li>  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 </li> Press ALT+Q to leave the Visual Basic Editor and return to Microsoft Excel (or Microsoft Word).</li> On the Tools menu, point to Macro, and click Macros. Select the Main macro and click Run. The Processing_Dialog dialog box appears.</li></ol>

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:

<ol> Activate the Visual Basic Editor. On the View menu, click Project Explorer.</li> Select the project that you created in the previous steps.</li> Right-click the "Processing_Dialog" UserForm and click Export File. Save the UserForm as "Processing_Dialog.frm."</li> Right-click the "Processing_Code" Module and click Export File. Save the module as "Processing_Code.bas."</li> Return to Microsoft Excel and open the workbook in which you would like to use the "Processing, Please Wait" dialog box.</li> Activate the Visual Basic Editor.</li> On the File menu, click Import File, select the "Processing_Dialog.frm" file, and click Open.</li> On the File menu, click Import File. Select the "Processing_Dialog.bas" file, and click Open.</li> <li> On the Insert menu, click Module, and type the following code in the Code window: Sub Main StartProcessing " ", " " End Sub Where the argument is the text string that you want to display in the Processing_Dialog dialog box and 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 " ", run the macro Main. </li></ol>

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:

148209 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 kbhowto kbprogramming KB162257

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.