MS Word

Remove Unnecessary Commands from a Recorded Macro

The Problem:

Hey, that was easy! I recorded a long macro that sets up the tedious bits of the proposal to a client. The trouble is, I got so carried away that I saved the document and closed it without stopping the Macro Recorder, so now each time I run the macro, it tries to save the active document under the same name.

The Solution:

Right: time for you to bite the bullet and come to grips with the Visual Basic Editor. Don't worry, it doesn't bite; it tries to be friendly, and it threatens you with neither AutoCorrect nor AutoFormat As You Type.

You can quickly toggle into the Visual Basic Editor by pressing Alt+F11 from Word, but it's usually best to display the Macro dialog box, select the macro you want to edit, and then click the Edit button. That makes the Visual Basic Editor open the macro you want to edit, so you don't have to navigate to reach it. Figure 8-2 shows the Visual Basic Editor with a recorded macro open.

Figure 8-2. The Visual Basic Editor interface is fairly easy to come to grips with.

In the upper-left corner of the Visual Basic Editor window is the Project Explorer, which you use to navigate among open projects. A project is the VBA component of a document or template. Word places all recorded macros in the NewMacros module in the Normal projectthe VBA component of Normal.dot. A module is a container for VBA code.

Below the Project Explorer is the Properties window, which shows the available properties (attributes) of the selected item. In this case, the NewMacros module is selected, and the only available property is its (Name) property.

The main area of the Visual Basic Editor is the Code window, which shows either the code for the selected item (here, the NewMacros module) or the selected user form. (A user form is a custom dialog box.) Each macro begins with a Sub line that shows the name of the macro followed by empty parenthesesfor example, Sub Format_MD_Report() and ends with an End Sub line.

To change your recorded macro, you edit the appropriate commands in the Code window. In this case, look for the commands for saving the document (the lines starting with ActiveDocument.SaveAs; an underscore at the end of a line indicates that the following line is a continuation) and closing it (the ActiveDocument.Close command). Select these lines using the mouse or the keyboard, and press the Delete key to delete them.

If you're satisfied with your changes, save them (click the Save button on the Standard toolbar, press Ctrl+S, or choose File » Save), and then choose File » Close and Return to Microsoft Word to close the Visual Basic Editor and return to Word. Alternatively, press Alt+F11 to flip back to Word, leaving the Visual Basic Editor open so that you can return to it later if you need to make further changes.

By default, the Visual Basic Editor displays your code in Courier New one of the clearest fonts to read, but also one of the most boring. To use a different font or size, choose Tools » Options, click the Editor Format tab, choose the font in the Font drop-down list, and choose the size in the Size drop-down list. You can also change the default colors for different types of code if you wish.