|
Idiots' Guide to Installing
Macros |
|
|
|
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
macros in Word. |
|
|
Let's use a macro to update all the fields in the document as an example.
The listing is as follows:
Sub UpdateAll()
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory
Then
While Not (oStory.NextStoryRange
Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
End Sub |
| 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. |
| |

Click the
developer tab then Macros to access the macros dialog.
 |
|
Note:
|
In both
Word formats ALT+F8 will give access to the following dialog.
The
illustration below is from Word 2003. The Word 2007 dialog is almost
identical and has exactly the same functions. |
| |
When you open this dialog, the first macro already
present will be highlighted in the Macro Name
box at the top of the window. Overwriting this name
will not delete that macro. 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' - as here - the macro will be saved in
the normal template. |
|
3 |
Click 'Create'.
If you have
entered or selected a name that already exists in the
active template, you will get a warning message:
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:
Sub macroname()
'
' macroname Macro
' Macro created date by user
'
End Sub |
|
 |
|
5 |
Copy the complete block of code from the
newsgroup message to the clipboard |
|
6 |
Switch to the macro editor window. Select all the above listed block
(see 4) and paste the contents of the clipboard over it.
If any lines are highlighted in red, this is usually an indication that the line in question has broken prematurely - as a result of e-mail editor line wrapping
e.g.

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.

Some listings do not include the
Sub ... End Sub lines. In this case, give the macro a useful descriptive name at
(1) above and paste the code between the Sub and End Sub lines. |
|
7 |
Click the 'Save' icon
on the vba editor toolbar and close the editor. |
|
Modules |
|
|
Word's VBA
editor files macros by project. In this case the project is Normal (ie
the Normal.dot 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 Module.
The macro shown in the illustrations is a variation on the example code
listed above.
  |
|
Add the
Macro to a toolbar (Word 2000-2003). |
| |
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).
In the illustration below the macro has not been
moved from the NewMacros module. If you are going to move the macro to
its own module, ensure that you do so before attaching it to a toolbar
button. |
|
 |
| |
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. |
| Add Macro to the
Quick Access Toolbar (Word 2007) |
| |
Word 2007
allows 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'.



 |
|
Auto... macros |
| |
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 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. |
| |
Sub
AutoNew()
ActiveWindow.ActivePane.DisplayRulers = True
'display the rulers
ActiveWindow.ActivePane.View.ShowAll = False
'turn off the formatting command view
With ActiveWindow.View
.Type = wdPrintView
'select page/print layout view
'Alternative to the above line if
normal view is preferred
'.Type = wdNormalView
.Zoom.Percentage
= 500 'fix the tiny cursor error
.Zoom.Percentage = 100
'set the display zoom to 100%
.FieldShading =
wdFieldShadingWhenSelected 'set
the field shading preference
.ShowFieldCodes =
False
'turn off field code display
.DisplayPageBoundaries =
True
'Turn on white space between pages
End With
CommandBars("Reviewing").Visible = False
'Turn off the reviewing toolbar
End Sub
Sub AutoOpen()
'Display filename and path in window title bar
ActiveWindow.Caption = ActiveDocument.FullName
'The remaining settings are the
same as for autonew
ActiveWindow.ActivePane.DisplayRulers
= True
ActiveWindow.ActivePane.View.ShowAll = False
With ActiveWindow.View
.Type = wdPrintView
.Zoom.Percentage
= 500 'fix the tiny cursor error
.Zoom.Percentage = 100
'set preferred zoom
.FieldShading =
wdFieldShadingWhenSelected
.ShowFieldCodes =
False
.DisplayPageBoundaries =
True
End With
End Sub
Sub AutoExec()
CommandBars("Reviewing").Visible = False
'Here after a short delay another macro is run
'This forces off the
display of formatting commands
'It may be necessary
to increase the delay for a particular PC.
Application.OnTime _
When:=Now + TimeValue("00:00:01"), Name:="CodesOff"
End Sub
Sub CodesOff()
'This macro is called
from AutoExec
On Error GoTo Oops
ActiveWindow.ActivePane.View.ShowAll = False
Oops:
End Sub |
| |
|