
The Anchorage
Personal website of Gregory K. Maxey, Commander USN (Retired)
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!
The purpose of this Microsoft Word Tips & Microsoft Word Help page is to show you a basic method to install and employ the many various VBA procedures (macros) that you will find in my collection of tips or in other Microsoft Word Help forums.
The VBA procedure (macro) shown below performs the common and often requested task of updating document fields. It can be used in all versions of Word (Word 97 through 2010). We will use it in the discussion that follows.
Public Sub UpdateAllFields()
Dim rngStory As Word.Range
Dim lngJunk As Long
Dim oShp As Shape
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
On Error Resume Next
rngStory.Fields.Update
Select Case rngStory.StoryType
Case 6, 7, 8, 9, 10, 11
If rngStory.ShapeRange.Count > 0 Then
For Each oShp In rngStory.ShapeRange
If oShp.TextFrame.HasText Then
oShp.TextFrame.TextRange.Fields.Update
End If
Next
End If
Case Else
'Do Nothing
End Select
On Error GoTo 0
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
lbl_Exit:
Exit Sub
End Sub
To use this macro it must be installed in a VBA project that is available to the document. To install and employ this macro perform the following steps:

Notes:
1. In this example I am using Word 2007. The dialog is similar in all Word versions.
2. To make a macro available to "all" documents, you install it in a global template. The "Normal" template is the global template "always" loaded and available in any Word document.
3. I typically have dozens of macros in my global Normal template. For clarity I've removed these macros temporarily so you can see how things will look installing your first macro.

Notes:
1. If not displayed, press CTRL+r to display the top left "Project" pane.
2. If not displayed, press F4 to do display the bottom left "Properties" pane.
3. Use the drag bar between the panes to adjust as appropriate.
4. The large white panel on the right is the "Code" pane.
5. The macro is created in a new standard module "New Macros." The module is stored in the modules folder as part of the Normal template. As you realize the benefits of macros and install more or begin creating your own you will want to organize your macros for more efficient management. See: Organizing your macros.
Note: If the code you have copied from a support forum or other source is just a snippet (i.e., not a complete procedure) then paste your snippet in between the procedure type/name declaration statement and the End Sub statement.


Note: The purpose of the "Option Explicit" statement is beyond the scope of this tips page. If it is there leave, if it is not don't worry about it. If you are like me and just have to know what it is all about then see: Why variables should be declared properly.
There is almost always more than one way to skin a cat. The basic method should be fool proof, but as you gain familiarity with the VBE you will discovery other methods. I typically bypass the "Macros" dialog (ALT+F8) altogether and go straight to the VBE using the shortcut ALT+F11. From there:
Now that your macro is installed to a global template it is ready for immediate use whenever you want to initiate it. Macros can be initiated in several ways. The most basic method is to use the same "Macros" dialog used to create the macro.

Note: The illustration above shows the new macro with several of the macros that I've restored to my Normal template.
With each version of Word there has been changes to the user interface (UI) and the location of the controls for working with macros. However, the ALT+F8 shortcut key specifically and the "Macros" dialog has remained consistent. It you think of the VBE as the "world" of VBA and macros (and it is), the you can think of the "Macros" dialog as the window to that world.
Notes:
1. See my: Add Macro to QAT if you would like to add a macro to the Word 2007/2010 Quick Access Toolbar.
2. If you would like to see a more on using the UI controls associated with macros see MVP Graham Macro's: Installing Macros from Listings.
3.
See: Setup Menu/Toolbar for instructions on how to run a macro from a menu or
toolbar icon.
4. See: Keyboard Shortcut for instructions on how to run a macro using a
keyboard shortcut.
In the past, I've always simply referred visitors to the MVP FAQ Organizing Your Macros for information on this subject but as the MVPs have apparently neglected to keep it up to date, I have decided to supplement the article with additional information here.
Please do refer to the linked article. It does contain valuable suggestions for keeping your macros organized plus tips for editing your macros that I will not repeat here. Where that article now falls short is in the heading "Organizing Your Global Templates." Particularly, loading global template add-ins.
To load a global template add-in when Word starts, place the template in Word's Startup folder. Word's Startup folder can be determined using:
To manually load a global template add-in, open the Templates and Add-ins dialog:
When the dialog opens, click the "Templates" tab. The Templates and Add-ins dialog is similar in all Word versions and is shown below.

Click the "Add" button, and using the "Add Template" dialog displayed, navigate to the template location and click "OK"


That's it! I hope you have found this tips page useful and informative.
Do you want to make a payment for consulting work or donate to help support this site?
PayPal is a safe, easy way to pay online.
Use the appropriate currency "Donate" button to make a payment or donation.