Create a macro

You can create a macro (macro: An action or a set of actions that you can use to automate tasks. Macros are recorded in the Visual Basic for Applications programming language.) by using the macro recorder to record a sequence of actions, or you can create a macro from scratch by entering Visual Basic for Applications (Visual Basic for Applications (VBA): A macro-language version of Microsoft Visual Basic that is used to program Microsoft Windows-based applications and is included with several Microsoft programs.) code in the Visual Basic Editor (Visual Basic Editor: An environment in which you write new and edit existing Visual Basic for Applications code and procedures. The Visual Basic Editor contains a complete debugging toolset for finding syntax, run-time, and logic problems in your code.).

You can also use both methods. You can record some steps and then enhance them with additional code.

ShowBy recording steps

  1. On the Tools menu, point to Macro, and then click Record New Macro.
  2. In the Macro name box, type a name for the macro.
  3. In the Store macro in box, click the template (template: A file or files that contain the structure and tools for shaping such elements as the style and page layout of finished files. For example, Word templates can shape a single document, and FrontPage templates can shape an entire Web site.) or document in which you want to store the macro.
  4. In the Description box, type a description for the macro.
  5. If you don't want to assign the macro to a toolbar (toolbar: A bar with buttons and options that you use to carry out commands. To display a toolbar, press ALT and then SHIFT+F10.), a menu (menu: A list of commands that is displayed when you click a menu name on a menu bar or other toolbar.), or shortcut keys (shortcut key: A function key or key combination, such as F5 or CTRL+A, that you use to carry out a menu command. In contrast, an access key is a key combination, such as ALT+F, that moves the focus to a menu, command, or control.), click OK to begin recording the macro.

To assign the macro to a toolbar or menu, click Toolbars, and then click the Commands tab. In the Commands box, click the macro you are recording, and drag it to the toolbar or menu you want to assign it to. Click Close to begin recording the macro.

To assign the macro to shortcut keys, click Keyboard. In the Commands box, click the macro you are recording. In the Press new shortcut key box, type the key sequence, and then click Assign. Click Close to begin recording the macro.

  1. Perform the actions you want to include in your macro.

When you record a macro, you can use the mouse to click commands and options, but not to select text. You must use the keyboard to record these actions. For example, you can use F8 to select text and press END to move the cursor to the end of the line.

  1. To stop recording your macro, click Stop Recording Button image.

ShowBy using Visual Basic for Applications

  1. On the Tools menu, point to Macro, and then click Macros.
  2. In the Macros in list, click the template (template: A file or files that contain the structure and tools for shaping such elements as the style and page layout of finished files. For example, Word templates can shape a single document, and FrontPage templates can shape an entire Web site.) or document in which you want to store the macro.
  3. In the Macro name box, type a name for the macro.
  4. Click Create to open the Visual Basic Editor.

 Note   If you give a new macro the same name as an existing built-in command in Microsoft Word, the new macro actions will replace the existing actions. To view a list of built-in macros in Word, point to Macro on the Tools menu, and then click Macros. In the Macros in list, click Word Commands.

 
 
Applies to:
Word 2003