Recording Simple Macros

Word, Excel, and PowerPoint all let you record macros. Access, and FrontPage do not have macro recording capabilities—a significant restriction, because you won't be able to use the recorder to capture VBA commands that correspond to typical user interactions. Although Outlook lacks a macro recorder, you can record macros while creating a message.

In theory, when you turn on the macro recorder, VBA "watches" as you perform some action or series of actions. When you turn off the recorder, you can replay the resulting recorded macro to replicate that series of actions.

In practice, you'll more often use the macro recorder to eliminate the tedious steps of creating a macro. Unfortunately, a recorded macro rarely solves a real-world problem by itself. After recording a macro, you'll typically need to make some modifications.

You can also use the recorder to capture the steps of a particular task, and then copy all or part of the recorded macro into a larger macro.

How the Macro Recorder Captures Actions

As anyone who's used Office's macro recorder for more than a few minutes can tell you, the macro recorder can't record every single action you take. There are two fundamental reasons why the recorder can fail:

  • The action you take might not have an exact translation in the application's object model. For example, if you record a macro in PowerPoint to change first-level bullet points in a presentation to 18-point bold, the macro won't work because PowerPoint's object model doesn't include commands for working with first-level bullet points.

Caution

This type of failure, generally completely undocumented, happens without any warning to you. The recorder doesn't stop; there's no other feedback. You know the failure occurred only because the macro fails to work when you play it back.


  • The action you take might be ambiguous; in other words, the recorder might not be able to tell exactly what you want to do. For example, if you type this paragraph into a Word document and use the mouse to select it, the VBA/Word macro recorder has no way of knowing what you're trying to do. Are you selecting the current paragraph? Or are you selecting the first paragraph that starts with the word "The"? Maybe you really want to select the 10th paragraph in the document. Or the first one with more than a hundred words. That's why the recorder usually won't record mouse actions—there's just too much ambiguity, most of the time, when you use the mouse.

After you turn on the macro recorder, it records the effect of your actions, not the actions themselves. The full effect of your actions goes into the recorded macro, not the means you used to apply them. For example

  • If you choose File, Open, type mydoc, and click OK, the recorder notes that you opened Mydoc.doc—not that you went through all the clicking.

  • If you choose Format, Font, and change the font to Wingdings, the recorder records the fact that you changed the font to Wingdings—but it also picks up all the other formatting settings, including font size, bold, italic, underline, and so on.

  • If your insertion point is inside a paragraph in a Word document, and you want to tell the recorder to select the first word in that paragraph, double-clicking the first word in the paragraph will not work. If you try to double-click the first word in the paragraph, the recorder won't let you do it. The recorder can't record your double-click action because it's ambiguous: You know that you want to select the first word in the current paragraph, but there's no way to specify that precisely by clicking with the mouse. For all the recorder knows, you might want to select the 50th word on the page, or the first word on the 10th line, or the last capitalized word in the paragraph.

When recording, instead of using the mouse, you'll frequently have to resort to obscure keyboard navigation keys. To move to the beginning of the current paragraph in Word, press Ctrl+↑. To select the first word in the paragraph, press Ctrl+Shift+→. To italicize the word, click Ctrl+I.

Tip from

Nobody, but nobody, memorizes all of Word's obscure key combinations. To create a 10-page document listing them all, click Tools, Macro, Macros, type listcommands, and press Enter. Click Current Menu and Keyboard Settings, and then OK. Unfortunately, there's no easy equivalent for PowerPoint or Excel.


Recording a Macro

Word, Excel, and PowerPoint include simple macro recorders that work in essentially the same way. To record a macro in Word, for example, follow these steps:

  1. Create a new document or open an existing document.

  2. Choose Tools, Macro, Record New Macro. In the Record Macro dialog box (see Figure 38.1), click in the Macro Name box, and type a name (ItalicizeFirstWord, in this example).

    Figure 38.1. Replace the generic Macro1 name with a descriptive macro name, but don't use spaces or punctuation marks.

Note

Macro names can contain up to 255 letters and numbers, but no spaces or other punctuation marks. Names must start with a letter, and cannot duplicate certain reserved names (for example, cell addresses in Excel).


  1. Choose a location for the macro (the current document or a template, for example) and add a description (optional).

  2. You'll see the Recording pointer, which has a picture of a cassette tape attached to the bottom. In addition, the Stop Recording toolbar appears on the screen (the Excel and PowerPoint versions of this toolbar are slightly different, but both include a Stop Recording button). Perform any actions you want to record in your macro.

  3. Click the Stop Recording button on the Stop Recording toolbar.

To record macros in Excel and PowerPoint, follow the same steps.

Testing the Macro

After recording a macro, it's essential that you test it to see whether it works the way you expect. To quickly run a Word macro, follow these steps:

  1. Open a document or create a new document. If necessary, click to position the insertion point at an appropriate location in the document.

Caution

Don't use a "live" document when testing. Always work with a backup copy or a dummy document you create just for testing.


  1. To run the macro, choose Tools, Macro, Macros. You'll see the Macros dialog box shown in Figure 38.2.

    Figure 38.2. All available macros appear in the Macros dialog box.

  2. Click the name of the macro you want to run and press Enter or click Run. If all goes well, the macro performs the task you intended.

  3. For more complete troubleshooting, click in another location within the document, and repeat steps 1–3.

Using the Macro dialog box lets you run all currently available macros, whether you're working in Word, Excel, PowerPoint, Outlook, Publisher, or FrontPage. If you're going to use the macro regularly, however, this procedure is cumbersome and slow; you'll learn faster methods for running macros later in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.253.62