Tutorial 3: Editing a Word macro

This tutorial takes you through:

  • viewing the code produced when a macro is recorded
  • simple editing of a macro
  • deleting a macro
  • copying and renaming a macro.

To be able to complete this tutorial, you need to have completed Tutorial 1 in which you recorded two Microsoft Word macros, MakeBold and MyName.

Viewing a macro

1On the Tools menu, select Macro and then click on Macros… which brings up a dialog box similar to the one below.

Macros dialog box

2In the Macros in dropdown box seen in the figure above, select the location to specify which macros will appear in the Macro name box. You can choose to display either a list of all the macros available in all the active templates or a list of all the macros stored in one particular template.

3In the Macro name box, click the name of the macro you want to edit. Then click the Edit button at the mid-right of the figure above. This will bring up a Visual Basic Editor window similar to the one shown below.

Visual Basic Editor

4If the Project Explorer is not visible, use Control + R or the menu sequence View/Project Explorer to open it. Remember, this allows you to access Visual Basic code in any module of an open template or document.

The macro created in Tutorial 1 should look something like this:

VBA code for the MyName macro

Editing the macro

Interpreting the code

Notice how the sections of the code are indented, which makes it easier to read:

Look at Selection.ParagraphFormat.SpaceBefore = 6

It’s a good idea to read this backwards. It translates to ‘Set the SpaceBefore value of Paragraph Formatting for the Selection to 6.

Removing unnecessary arguments

Remember that unnecessary arguments can be removed, making the macro easier to understand and in some cases faster to run. The above macro can be reduced to the code below by simply deleting the unwanted lines.

VBA code for the MyName macro after removing unnecessary code

How do you know which arguments to remove? Generally all the ones that you did not alter, such as:

.LeftIndent = CentimetersToPoints(0) or

.RightIndent = CentimetersToPoints(0)

Make sure to remove the period before the argument. If you leave in the punctuation characters by mistake, the macro will not run and an error message will be displayed.

Deleting statements

If you remove a With, be sure to remove the whole section including the End With.

Disabling code

If you want to disable part of the code without deleting it, place a ‘ (an apostrophe or single quote) in front of the statement or argument.

Deleting a macro

You may find that you record lots of macros that you don’t want.

2On the Tools menu, select Macro and click on Macro as seen in the following figure.

Macros dialog box

2In the Macros in box, click an item to specify which macros will appear in the Macro name box. You can choose to display either a list of all the macros available in all the active templates or a list of all the macros stored in one particular template.

3In the Macro name box, click the name of the macro you want to delete.

4Click on the Delete button.

Copying and renaming a macro

You know that macro code is stored in modules. Here’s the Visual Basic Editor window again, displaying two macros. (Remember that you can open this window by selecting Tools/Macro/Visual Basic Editor)

Visual Basic Editor

In the figure above you can see that all macros are stored in a module called NewMacros. This module is stored in the normal template.

Individual macros can be copied and pasted within the Visual Basic Editor.

Using the Organizer

The whole module can be copied to another template using the Organizer.

To open the Organizer from within any recent version of Word follow the menu sequence Tools/Macro/Macros/Organizer. See the figure below which show the Organizer dialog box.

Macro Organizer dialog box

Renaming a module

You can delete or rename a module using the Organizer.

If you want to change the name of a macro, you can rename it in the Organizer dialog box shown above. Then you should open the code by using the VBA Editor, and replace the text after the Sub

For example, you might want to replace Sub NoBorder() with Sub NoLine().

You should also change NoBorder in the green comment area of the code; however, this will have no effect on the operation of the macro.

When you rename a template you will have to reassign the macro to menus, toolbars and shortcut keys.

You should save the module after making these changes. Remember that you can also cut and paste code both within and between modules.

Note!

  • A macro begins with Sub and ends with End Sub. Be careful if you are copying code to take the entire macro’s code or a complete code segment.

Never have two Subs with the same name after them.

Practice activity 1

Edit the MyName macro you created earlier so as to remove all of the unnecessary code. Test that it still works as expected.

Feedback

Does the macro still work? You need to be extremely careful as to the punctuation that you keep and delete:

  • Excess spaces between argument names are easily missed as in.SpaceBefore and.Space Before.
  • Spelling must be perfect as this is the only way that Visual Basic knows what you want done so check your spelling letter by letter if necessary,.SpaceBefore easily is mistyped as.SpacesBefore.

Practice activity 2

Create a macro that will insert a header and footer to new documents called HeadFoot. The header should display your name and the footer should contain the page number and subject name.

1.Add this macro to a new toolbar.

2.Assign a shortcut key to this new macro.

3.Add the macro to the Format menu as the first menu option.

4.Edit the macro and remove any unnecessary coding.

5.Test that the macro works from all these places even after the removal of the code.

6.Demonstrate the macro to a willing participant. You could ask them to play the role of a client, then ask them for feedback. Discuss their feedback and make changes to the macro if you think they would be useful.

Feedback

This activity gives you revision on all the tasks you have been learning about. Was it easy to identify the unnecessary code? If you are in doubt don’t remove it. However you may like to try recording a second macro that reverses all the changes. By comparing the two macros, you can identify arguments that are identical in each indicating that these arguments are not changing and so can be removed.

Hint: You will usually only remove whole lines between the with and end with lines.

Practice activity 3

Here you’ll practise using the Organizer.

1Create a new blank template called Macros.dot. There is no need to put any text into the template. Its only purpose is to transport macros between computers.

Tip: The lack of text in the main document can lead to the accidental deletion or loss of changes so it is good idea to type a note to yourself about the nature of the file and its purpose.

2Use the Organizer to copy the module that contains your macros from the normal template to Macros.dot.

3Use the Visual Basic Editor to rename some of your macros in the Macros.dot template. Be careful not to rename those in the normal template.

4Copy Macros.dot to a floppy disk and transfer the template to another computer.

5Use the Organizer to copy the module that contains your macros from Macros.dot to the normal template on the new computer.

6Open a new document and check that the macros are available. (Tools/Macro/Macros)

Feedback

Did you take the advice from the tip above? Beginners often edit the macros in a file then when asked to save a document while exiting they don’t realise it means the macros as well. You are often prompted to save the changes to the normal template and many people say no because they want to leave the normal template alone but end up losing all their macros.

The floppy disk version of Macros.dot now is a backup of your macros you have created. It becomes a very useful resource and many client support people collect macros from many sources as tools and templates for solutions to problems they haven’t even encountered yet.

1

2005