Implementing a Progress Bar in Word VBA
Article contributed by Ibby
When the code you've written takes a long time to run, you may find it useful to display a UserForm with a Progress Bar that shows the user the progress of the operation. The following illustrates an example of how to create such a UserForm.
1. |
Create a new project (open a new document). |
||||||||||||||||||||
2. |
Insert a UserForm (“UserForm1”) and a Module (“Module1”) |
||||||||||||||||||||
3. |
On the UserForm, place a Frame and place a Label inside the Frame. Change the following properties of the Frame and Label: Frame1
Label1
|
||||||||||||||||||||
4. |
In Module1, paste the following code: Option Explicit |
||||||||||||||||||||
5. |
In the UserForm1 module, paste the following code: Option Explicit |
||||||||||||||||||||
6. |
Now Run the macro TestProgress to see how this works. |
Note that if you run the macro from the VB Editor, the progress bar may still look a little strange, even using the Me.Repaint line; but it looks fine if you run the macro from Word – as a user would do – by clicking a toolbar button or a shortcut key, or by selecting Tools + Macro + Macros + Run.
It is much better to use Me.Repaint rather than the alternative of DoEvents, because the latter is very resource-hungry.