Installing Macros From Listings

Home Up Search This Site What's New? Audio On CDR Favourites Downloadable files Photo Gallery 2002 Photo Gallery 2003 Photo Gallery 2004/5 Photo Gallery 2006/7 Photo Gallery 2008 Photo Gallery 2009/10 UK Photo Gallery Ireland Photo Gallery Cats Photo Gallery 

 

Google
 

Many people access the material from this web site daily. Most just leech what they want and run. That's OK, provided they are not selling on the material as their own; however if your productivity gains from the material you have used, a donation from the money you have saved, however small, would help to ensure the continued availability of this resource.

Click the appropriate button above to access PayPal.

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 Button image. 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.

Shortcut Tip

 

When creating a new macro via the Macros dialog, you can dictate which module to save the macro in (or create a new module) by adding the module name as a prefix to the macroname, separated with a full stop (period) as in the following illustrations. Here the macro name is 'MacroName' and the module name is 'MyMacros':

Adding References to Other Applications

 

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 the vba editor, and the process is identical for both Word 2007 and earlier versions - though the range of available references will be different.

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 a keyboard shortcut?
 

Open the customize dialog (see previous section).

 

Note:

Shortcuts saved in the normal template will be available to all documents. Shortcuts saved in the active document will be available only to that document.

 

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'.

Add a keyboard shortcut?
 

Keyboard shortcuts may be used in addition to, or instead of, QAT commands. From the same dialog, click the 'Customize' button as shown.

Note:

Shortcuts saved in the normal template will be available to all documents. Shortcuts saved in the active document will be available only to that document.

 

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 (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.

  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

Note: You will find more macro examples on the vba macro examples page