Elsewhere in this web site, and in the Microsoft Word newsgroups you may be offered macro listings. Here is a simple guide to installing such listings as macros in Word.
Let's use a useful macro to update all the fields in the document as an example. The listing is as follows:
1. In Word 2000-2003, select Tools > Macro > Macros
In Word 2007, access to the macro dialogs has been changed and as installed is not available. To make it available, it is necessary to first add the 'Developer' tab to the Ribbon. To do this, Click the Microsoft Office Button. That's the one that looks like a pizza.
Then select 'Word Options'
Click 'Popular', and then select the 'Show Developer tab in the Ribbon' check box.
For Word 2010 onwards, the 'Developer' tab should already be shown by default, but if not, it is accessed via its the integral ribbon editor. In these applications the ribbon editor is available from File > Options > Customize Ribbon.
The illustration below is from Word 2016. Earlier Word version dialogs are almost identical and have exactly the same functions.
When you first open this dialog, and assuming that you have no add-ins installed, and there are no macros in the current document, the dialog will be empty as shown below:
Type the Name of the macro into the Macro Name i.e. the bit after Sub and before () here UpdateAll - (no spaces in the name!) box.
2. In the field directly beneath the macro names list box, pick which template or document you wish the macro to be saved in.
If you select 'All active templates and documents' - the macro will be saved in the Normal template, which for Word versions up to 2003 is Normal.dot, and for Word from version 2007, it is Normal.dotm.
3. Add or edit the description, if you wish, then click 'Create'.
If creating a new macro, change the macro name to a unique name.
4. The macro editor will open at the correct place. There will be an entry as follows.
5. Copy the complete block of code from the forum message to the clipboard
6. Switch to the macro editor window. Select all the above listed block from Sub to End Sub inclusive. Do not select anything above Sub, such as Option Explicit as shown here.
And paste the clipboard contents over the selected text to give the following.
You can tab the code to make it easier to follow if you wish, as shown below, but it is not strictly necessary for the purposes of this example.
Move to the start of the following line and backspace to join that line to the end of the red highlighted line as shown below.
7. Click the 'Save' icon on the VBA editor toolbar and close the editor.
Word's VBA editor files macros by project. In this case the project is Normal (i.e. the Normal template). Projects are sub-divided into Word Objects, Modules and Forms. By default, macros are stored in the Normal project in a module called NewMacros. Although it is not essential at this level, Modules make macros much simpler to handle, so it is good practice to separate macros you create into Modules.
If they are not already displayed, from the VBA editor view menu, display the Projects and the properties Windows.
From the Insert Menu select 'Module'.
This will create a new module in the normal project called Module1
From the Properties window rename the module to something more meaningful - here UpdateAll
Then cut and paste the macro from the NewMacros module to the newly created Module1.
Although macros will work without specifically declaring the variables used, it is good practice to declare all the variables used in your macros. To force you to do so, you can use the command
at the start of each module, as shown in the above illustration. You can set an option in the VBA editor to insert this line automatically when you create a new module
Some macros may call procedures from other applications. You may wish to access (say) Outlook or Access objects from within a Word macro. There are some macros on this site that require the addition of a reference to Outlook's object library. Such references may be added from the Tools > References menu of theVBA editor, and the process is identical for all Word versions - though the range of available references will be different.
Where calls are made to pre-declared references, this is referred to as 'Early Binding'. It is also possible to declare such items as Objects and not add the reference to the object library. This is known as 'Late Binding'. There are advantages and disadvantages to both approaches, but for macros to be used on your own PC, especially while learning to use VBA, I would stick with Early Binding.
For a discussion of the two systems, see https://support.microsoft.com/kb/245115.
From Word's document screen, right click on the toolbar area and select 'Customize' (or pick Customize from the Tools menu) then from the command list left window select macros.
In the right window pick your newly created macro with the left mouse button and drag it to your toolbar and drop it where you would like it to appear. (Or drop it in an appropriate menu if you prefer).
You can create personal toolbars from this dialog. I strongly recommend creating personal toolbars to hold your personal tool groups.
Right click on the entry and edit the name to something sensible - for a toolbar button use an abbreviation and/or add a suitable icon
Close the 'customize' wizard and the macro will be available to your documents.
Open the customize dialog (see previous section).
Word 2007 and above allow regularly used macros to be added to the Quick Access Toolbar (QAT). Right click the QAT and select 'Customize Quick Access Toolbar'
At the next dialog, select 'Macros'.
Word 2010 and later additionally provide the option to add a macro to a ribbon button (It would also be possible to add the button to the ribbon in Word 2007 also, but that requires a more in depth knowledge of XML programming. If you wish to pursue that option, see https://gregmaxey.com/word_tip_pages/customize_ribbon_main.html)
The Word 2010/2016 ribbon editor is accessed from File > Options > Customize Ribbon. The principles involved are similar to those of editing the QAT (Quick Access Toolbar), but here you have more flexibility. My suggestion would be to add a new tab to the ribbon to provide your macro commands.
Keyboard shortcuts may be used in addition to, or instead of, QAT (Quick Access Toolbar) commands.
From the same dialog, click the 'Customize' button as shown.
Macros usually have to be called in order to run, but there are macros that run automatically when certain events present themselves. These are called auto macros:
AutoExec - runs when Word is started (from the start menu or desktop shortcut) or a global template is loaded.
AutoNew - runs on creating a new document.
AutoOpen - runs on opening an existing document.
AutoClose - runs when a document is closed.
AutoExit - runs on closing Word or unloading a global template.
These are well documented in Word's VBA help, so I don't propose to elaborate on what is written there. I will simply show a couple of examples.
Word 2003 in particular seems prone to losing at least some of its user settings at random, and so you can use auto macros to force the preferred conditions. The following are taken from my own laptop for Word 2003.