logo MakeOfficeWork.com
Search Office Sites
Powered by Microsoft Bing™

Word Macros: The Benefits

A macro is typically a series of commands or instructions that are combined to form a single command. Macros can save you time by letting you automate relatively simple tasks that you need to perform often, as well as complex procedures that consist of many steps. Macros can be powerful tools that can greatly reduce the time that you will need to finish your work and can eliminate the need to remember all the steps in a tedious procedure. Even if you know nothing about writing macros, you can create macros by using the Macro Recorder, which records the steps that you perform and translates them into macro code.

Everyone can use macros. You do not need any programming knowledge to use macros, and you can obtain macros without ever writing any code yourself. The macros that can help you do your work faster and easier can be obtained and installed in several ways, including the following.

  • Macros can be created by using the Macro Recorder, which records the steps that you perform and translates them into macro code.
  • If you can verbally describe what you want a macro to do, you can post a request to the Office Programming forum, and one or more of the experts who will read your request will be happy to write the macro for you.
  • The code of a macro can be copied as text from a reliable trustworthy source and added to the NewMacros module of a template, from which it will always be available.
  • Macros that are stored in a template that you obtain from a reliable trustworthy source can be made available by installing the template or by using the Organizer to copy the applicable module into an installed template.

Recording a Macro and Assigning a Keyboard Shortcut to It

To see how to record a macro and use it, consider the scenario in which you type two letters in the wrong order without creating a spelling error, as in the case of typing the word fro instead of the word for. One way to fix this error is to press Backspace twice and retype the correct letters. That solution requires four keystrokes, but the same correction can be done in one keystroke with a recorded macro assigned to a keyboard shortcut. Because a macro does not automatically remember the characters that are deleted when you press Backspace twice, when we record the macro, we will cut the second of the characters that were typed in the wrong order, place the cursor before the first of the characters that were typed in the wrong order, and insert the character that we copied to the clipboard in that position as described in the following procedure.

To record a macro and assign a shortcut key to it

  1. Type the word fro.
  2. Start the Macro Recorder.
    • To do this in Word 2010 or Word 2007, on the View tab, click the lower part of the Macros botton, and then click Record Macro.
    • Record Macro

    • Alternatively, in Word 2010 or Word 2007, you can also start the macro recorder from the Developer tab. To start the macro recorder from the Developer tab, in the Code group, click Record Macro.
    • If you are using Word 2010 and the Developer tab is not shown, on the File tab, click Options. Then in the Word Options dialog box, click Customize Ribbon, under Customize the Ribbon select the Developer check box, and click OK.

      If you are using Word 2007 and the Developer tab is not shown, click the Microsoft Office Button, click Word Options, click Popular, and under Top options for working with Word, select the Show Developer tab in the Ribbon check box.

      Developer tab

  3. In the Record Macro dialog box, in the Macro name box, replace the default name of the macro by a meaningful name, such as ReverseLetters.
  4. Record Macro dialog box

  5. Click the Keyboard button.
  6. In the Commands box, verify that the name of the macro that you are creating is selected.
  7. In the Press new shortcut key box, press Shift+Backspace or a different key or press key sequence that you want to use, and then click Assign.
  8. In the Store macro in box, leave the default setting.
  9. Click Close to start recording the macro.
  10. Press Shift+Left.
  11. Press Ctrl+X.
  12. Press Left.
  13. Press Ctrl+V.
  14. Press Right.
  15. Stop the recording of the macro.
    • To do this in Word 2010 or Word 2007, on the View tab, click the lower part of the Macros botton, and then click Stop Recording.
    • Stop Recording

    • Alternatively, in Word 2010 or Word 2007, on the Developer tab, in the Code group, click Stop Recording.
    • Stop Recording

Your new macro is now ready to use at any time by pressing Shift+Backspace. You can test it by reversing any two letters as you type any word, stopping after the reversed letters, and pressing Shift+Backspace. You can now press Shift+Backspace anywhere in a document to switch the order of the last two characters before the cursor.

Note. If the name that you assign to a macro is identical to the name of a built-in Word command, the actions defined in your macro will replace the actions of the built-in Word command. To view a list of the names of the built-in Word commands, press Alt+F8, and in the Macros in drop-down list, select Word Commands.

For information about creating a macro in Word 2013, including a procedure for changing the keyboard shortcut and the steps for adding the macro to the Quick Access Toolbar, see the Microsoft help topic Create or Run a Macro. For information about creating a macro in Word 2010, see the Microsoft help topic Record or Run a Macro. For information about creating a macro in Word 2007, including a procedure for changing the keyboard shortcut and the steps for adding the macro to the Quick Access Toolbar, see the Microsoft help topic Record or Run a Macro. For information about deleting a macro in Word 2007, see Delete a Macro.

Modifying an Existing Macro

The recorded version of the ReverseLetters macro uses the clipboard to store a letter. If you do not want the ReverseLetters macro to change the clipboard contents, you can replace the recorded version of this macro by the following manually revised version, which copies the second of the characters that were typed in the wrong order to a String variable instead of the clipboard.

Sub ReverseLetters()
    ' Macro that reverses the order of the last two characters
    ' before the cursor.
    Dim myChar As String
    
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    myChar = Selection.Text
    Selection.Delete
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeText myChar
    Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub

You can use the following procedure for modifying an existing macro that is stored in the default global template to replace the recorded version of the ReverseLetters macro by the manually revised version.

To replace the code of an existing macro by a revised version

  1. In your browser, select the code of the revised version of the macro after the first line, which contains the word Sub and the name of the macro, to end of the line containing End Sub, and then press Ctrl+C.
  2. Note that the first line of the macro is not copied together with the rest of the macro because replacing this line in the recorded macro would delete the keyboard shortcut that you defined.

  3. In any Word document, press Alt+F8.
  4. In the Macros dialog box, under Macro name, select the name of the macro that you want to modify and click Edit.
  5. In the Visual Basic Editor, select the code of the macro after the first line, which contains the word Sub and the name of the macro, to end of the line containing End Sub.
  6. Press Ctrl+V.
  7. Press Ctrl+S to save your changes and close (or minimize) the Visual Basic Editor.

It should be mentioned here that a macro may contain code which instructs Word to repeat an action until a certain condition is met. Such code is called a loop. If the circumstances are such that the condition is never fulfilled, Word will continue to execute the code within the loop indefinitely and will appear to hang. If Word appears to hang while you are running a macro, you can stop the execution of the macro by pressing Ctrl+Break and then clicking End.

Renaming, Deleting, and Copying Macros

As you continue to use macros, you may want to rename a macro, delete a macro, or copy macros to a template. The following procedures describe the steps needed to perform these tasks.

To rename a macro

  1. In any Word document, press Alt+F8.
  2. In the Macros dialog box, in the Macros in drop-down list, select the applicable template or document.
  3. In the Macro name box, select the name of the macro that you want to change and click Edit.
  4. In the Visual Basic Editor, in the first line of the macro, which begins with the word Sub, change the existing name to the new name, but do not remove the word Sub or the pair of parentheses at the end of the line.
  5. Press Ctrl+S to save your changes and close (or minimize) the Visual Basic Editor.

To delete a macro

  1. In any Word document, press Alt+F8.
  2. In the Macros dialog box, in the Macros in drop-down list, select the applicable template or document.
  3. In the Macro name box, select the name of the macro that you want to delete.
  4. Click Delete.

By default, the macros that you create in Word are stored in the NewMacros module of the default global template, which is Normal.dotm in Word 2007 or Normal.dot in earilier versions of Word. Macros can be stored in a template or in a document. Macros stored in the default global template are always available and can be run from any document. Macros stored in a document are available only when the document is active. To distribute macros to other users, copy the module containing them to a template that you created, distribute the template file, and instruct them to install the template in their templates folder. The macros will be available whenever a document based the template is the active document. Alternatively, you can instruct them to install the template in the Word Startup folder. To determine the location of these folders in Word 2007, click the Microsoft Office Button, click Word Options, click Advanced, scroll down to the General section, and click File Locations. A template that is stored in the Word Startup folder is a global template or an add-in. The macros in templates that are in the Word Startup folder can be called from any document.

To copy the macros in a macro module to a template

  1. In any Word document, press Alt+F8.
  2. In the Macros dialog box, click Organizer.
  3. In the Organizer dialog box, under the name of the active document, click Close File.
  4. Click Open File, and open the template to which or from which you want to copy macros.
  5. Click an item that you want to copy in either list, and then click Copy.
  6. Repeat the previous step until you have copied all the items that you want to copy.
  7. Click Close.

You can also delete or rename a macro module in the Organizer by selecting the applicable module and clicking Delete or Rename.

More Information

For detailed instructions on installing a macro that you found on a reliable website or received from a trustworthy source, see Idiots' Guide to Installing Macros. For links to pages with examples of macros and more information related to macros, see Word Macros and Visual Basic for Applications FAQ.

Quick Reference for this Page

This page demonstrates the benefits of macros by providing instructions for recording a simple macro that you may find useful and for assigning a keyboard shortcut to it. There are also procedures for modifying an existing macro by copying macro code as text from another source, and for renaming, deleting, and copying macros.

Search Office Sites

You can search the websites that have the most useful and valuable information about using Microsoft Office products by using the Search Office Sites search box in the banner. Click to start your search.

Other Resources