• No results found

Create and modify simple macros

A macro is a procedure that uses the Microsoft Visual Basic for Applications (VBA) programming language. A procedure is a series of instructions that are saved as a unit, which can then be performed as a single instruction.

This macro substitutes character styles for local font colors

In Word, you can record a series of commands (including settings you select in a dialog box) or keystrokes as a macro. When you create a macro, you perform the series of actions that completes a task or a portion of a task that you want to auto-mate, and Word records the tasks. You can then run the macro and have Word perform the tasks for you.

IMPORTANT While you record a macro, you can use the mouse to invoke commands but not to select objects, because selecting an object with the mouse isn’t a step that Word can re-create.

Objective 4.1: Create and modify building blocks, macros, and controls Objective 4.1: Create and modify building blocks, macros, and controls

4

The first step in recording a macro is to specify settings for the macro, including a name and a location. You can save a macro in Normal.dotm, which makes the macro available in all documents, or in the template file that is attached to the current docu-ment. You can assign a macro to a keyboard shortcut or to a button that you can place on the Quick Access Toolbar or on the ribbon.

Before you record a macro, it can be helpful to rehearse the steps you want to record. As an example, you might record a macro that applies a set of page-layout options, such as inserting a section break and setting the margins, page orientation, and header for the section. You could run a macro such as this in documents whose content requires pages that use the Landscape orientation (to display a wide table, for example) in addition to pages that retain the Portrait orientation. As preparation for recording the macro, walk through the options that you want to apply and the sequence in which you want to apply them.

See Also For more information about page layout options, see “Set advanced page layout options” in “Objective 2.1: Perform advanced editing and formatting.”

You can take your time performing the actions while recording the macro because Word doesn’t record the timing. You can pause recording when you need to deter-mine which command or option to select next or to modify a setting that you have already selected. When you pause recording, you can effectively edit the macro (so that it applies the correct setting) without having to work directly with the VBA code that is generated by Word.

You can edit a macro that you have recorded by opening the Visual Basic for Applications editor and code window. In the code window, you might change the font color the macro applies, for example, or change the text that is used in a find-and-replace operation you performed when you recorded the macro.

To open the Record Macro dialog box

On the View tab, in the Macros group, click Record Macro.

On the Developer tab, in the Code group, click Record Macro.

On the status bar, click the Macro Recording button.

Tip The Developer tab is hidden by default. You display the Developer tab from the Customize Ribbon page of the Word Options dialog box. For information about displaying hidden tabs, see “Change default program settings” in “Objective 1.1: Manage documents and templates.”

Objective group 4 Create custom Word elements Objective group 4 Create custom Word elements

To record a macro

1. Open the Record Macro dialog box.

2. In the Macro name box, enter a descriptive name for the macro.

Tip Macro names cannot contain spaces. They can contain numbers, and words can be separated by an underscore character (_).

3. In the Store macro in list, select the template or document in which you want to save the macro.

Give macros names that describe their purposes

4. If you want to save a description of the macro, enter it in the Description box.

5. Click OK, and then perform the steps you want to record in the macro.

6. On the View tab, in the Macros group, click Stop Recording.

To pause and resume recording

1. On the View tab, in the Macros group, click Pause Recording.

2. Correct the previous setting or determine the next action to take.

3. On the View tab, in the Macros group, click Resume Recording.

To open the Macros dialog box

On the View tab, in the Macros group, click View Macros.

On the Developer tab, in the Code group, click Macros.

Objective 4.1: Create and modify building blocks, macros, and controls Objective 4.1: Create and modify building blocks, macros, and controls

4

To edit a macro

1. Open the Macros dialog box.

2. In list of macros, select the macro you want to change, and then click Edit to open the Visual Basic for Applications code window.

3. In the code window, edit the code statement you want to change.

4. Close the code window, and then close the Visual Basic for Applications window.