Tuesday, May 10, 2005

Editing Macros: Fear Not

Safety First

Word macros are stored in templates. You probably store yours in Normal.dot, the default template Word uses for all documents. A corrupted template can cause Word to misbehave—or stop functioning entirely—so it's a good idea to make a backup copy of your template before you do any substantial macro editing.

To save templates safely, first close Word if it is open and then use File | Save As to create a copy of Normal.dot with another filename. We copied Normal.dot to Normal.001 the first time we edited it, then Normal.002, and so forth. Using the date in the filename can also be useful. Such naming strategies provide a trail of fallback versions if they are needed.

Creating the Basic Macros

Let's say you have a set of documents you want to use for a client presentation, but you want to make them uniform. It is extremely tedious to go into each document and change the various elements, but macros can turn this into a quick and easy project. You start by recording a macro to change the fonts. Create a test document to work with, then start the Macro Recorder with Tools | Macro | Record New Macro and name the macro Client1. Now select the entire document with Edit | Select All and convert all text to the Arial typeface by selecting Arial from the drop-down Font list on the Formatting toolbar. If you're using Word 2002, you may think you're finished, but there is one more step: Select Arial from the nearby drop-down Style menu.

Next, record another macro named Copyright, which inserts a Word page footer containing a copyright notice into your documents. Begin recording, select View | Header and Footer, and click on the Switch Between Header and Footer button in the toolbar that appears. Type some text for your footer and click the Close button.

Inside the Macros

We now have two macros, Client1() and Copyright(). Let's take a look at them in the VB Editor. Select Tools | Macro | Macros, and in the dialog box that appears, select the Client1 macro name and click the Edit button. The document that appears actually contains all of the macros you've created.

Macros are in fact Visual Basic subroutines. A macro begins with a line containing the word Sub, the macro's name, and parentheses, as in Sub Client1(); it ends with the statement End Sub. Everything in between is the code that does the nitty-gritty work of the macro.

Take a look at Figures 1 and 2 , which show the Word 2000 and Word 2002 versions of Client1(), respectively. Both versions include unnecessary code that we can delete. In the Word 2000 version, it's easy to figure out what you can delete: A bug causes the Macro Recorder to insert the following line twice.

Selection Font.Name = "Arial"

You need only one such line, so delete the other. As always, be careful when deleting or adding lines, making sure that each line of the resulting code remains on its own line. When satisfied, select Save | Normal in the VB Editor, return to the Word window, and check your macro on a test document.

The Word 2002 version of Client1() contains a lot more code, because Word recorded all elements of the states you changed when you selected from the Font and Style pick lists, so you need to do a bit more sleuthing to determine what you can delete. We want to keep the Selection.WholeStory, which selects the entire document. But the lines between the first With Selection.Font and its corresponding End With have nothing to do with changing to the Arial font and thus can be deleted. The only line related to our goal occurs in the second With Selection.Font section: .NameAscii = "Arial". You can delete all the other lines in that section. The result should look like this:

Sub Client1()
' Client1 Macro
' Macro recorded 08/21/02 by J. W. Olsen
With Selection.Font
.NameAscii = "Arial"
End With
End Sub

How do you know what you can edit safely? To a large extent, this comes with experience and experimentation. But as in examples here, duplicate code may be wasted code, cluttering and slowing down a macro. Entire chunks of code, in fact (such as the With...End With block in the Word 2002 macro), may show no apparent connection to the desired goal of a macro and thus are candidates to test for deletion. You can comment a line out first to see what happens. And as long as you've saved backup versions, you can feel free to experiment.

Less Is More

You can now run both the Client1() and Copyright() macros on a set of Word documents separately. But that is tedious and potentially error-prone, especially if you're planning to create several macros.

One option is to record a new macro that does nothing more than invoke the existing macros. By doing so, you nest these macros within a new parent macro. The advantage of this approach is that if you change any of the original macros, such as changing the information in the Copyright() macro, the parent macro automatically inherits the updates, too. This could be a disadvantage, however, if you don't want your master macro to change automatically when you edit one of its child macros. Instead, you may want to combine your two macros into one.

With the VB Editor open, first copy everything in your Copyright macro between Sub Copyright() and its corresponding End Sub. (You don't need to include the Sub and End Sub lines.) Then, immediately before the End Sub line in the Client1() subroutine, use Edit | Paste to insert the copyright code. Your results should resemble Figure 3 for Word 2002.

The revised Client1() macro now changes a document's typeface and inserts the copyright footer. Your original copyright macro remains intact, so you can edit it without affecting the copyright notice in Client1().

As you can see, you can edit, copy, and paste within macros much the same way you can in an ordinary Word document. Just use extra care in the VB Editor: Unlike with ordinary text in a Word document, a minor typo in the macro code can render the macro—and possibly your entire template—useless. So don't forget to save a backup copy of your template.

No comments: