Use Macros to Automate Repetitive Tasks in Microsoft Word

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

 

Erik Olson
Microsoft Corporation

March 2002

Applies to:
     Microsoft® Word 97 and later

Summary: Learn how using macros can help you automate daily tasks and get you started learning how to program for Office. (11 printed pages)

**Note   **For security information, see Security Notes for Microsoft Office Solution Developers.

Contents

Introduction
Recording a Macro
Running a Macro
Assigning a Button to Your Macro
Assigning Your Macro to a Keyboard Shortcut
What Does the Code Look Like?
Conclusion

Introduction

If you've been working with Microsoft® Word for a while, and you're noticing that you keep doing the same task over and over, you must be wondering if there is a way to automate your work in Word. Macros may be the answer. A macro is a series of commands and instructions that you group together as a single command to accomplish a task automatically. That means Word can do your work for you. How cool is that?

What actions are the most common candidates for macros?

  • Combining multiple commands; for example, inserting a table with a specific size and borders and with a specific number of rows and columns.
  • Routine editing and formatting.
  • Making an item in a dialog box more accessible.
  • Automating a complex series of tasks.

Let's start with the first action, which is the table that you repeatedly create. Being an efficient Word user, you probably don't create it from scratch every time. You probably cut and paste a table into the document when you need it. This action requires you to keep the table in another document, opening the document, and then pasting it into the document you're working on. So, let's solve this problem programmatically; or in other words, let's get Word to do the work for us.

The first step in getting Word to do our work for us is to use the macro recorder.

Recording a Macro

Let's start by recording a macro in Word that, when finished, will insert a customized table into your document at the click of a button.

  1. Create a blank document in Word.

  2. On the Tools menu, point to Macro.

  3. Click Record New Macro.

    **Note   **You will notice the default setting for the Store macro in list is set to All Documents (Normal.dot). The behind-the-scenes coding for your macro will be stored in this Normal.dot template. As long as the Normal.dot template remains on your computer, you will be able to utilize the macros stored in this template. I will cover more about templates and storing macros in a future article.

  4. In the Macro name box, type a name for your macro. I named mine InsertTable1.

  5. Click OK.

You will see a toolbar appear that displays two buttons: Stop Recording and Pause Recording.

Aa140270.odc_beginner_macros_1(en-us,office.10).gif

Figure 1. The Recording toolbar

While you are recording, your actions will be recorded and can be used again as a macro.

**Note   **Mouse strokes may not always be recorded, so it's best to use the keyboard to access menus and do tasks that might otherwise be done with the mouse.

So, let's try the following:

  1. On the Table menu, point to Insert, and click Table.
  2. In the Number of Columns list, click 4.
  3. In the Number of Rows list, click 10.
  4. Click OK.

You will see the four-column, ten-row table appear in your document. Now, let's add headings.

  1. In the first cell of the first column, type Date.
  2. In the first cell of the second column, type Check Number.
  3. In the first cell of the third column, type Amount.
  4. In the first cell of the fourth column, type Sent To.

Now that you have headings, let's make them stand out a bit more with bold and background shading.

  1. On the Table menu, point to Select, and click Row.
  2. On the Format menu, click Font.
  3. With the Font dialog box displayed, click Bold in the Font Style list.
  4. Click OK.
  5. On the Format menu, click Borders and Shading.
  6. With the Borders and Shading dialog box displayed, click the Shading tab.
  7. In the Fill area, click on the color that you desire as the background color for your heading row.
  8. Click OK.

Now that you have your perfect table, you must stop the macro recorder by clicking on the Stop Recording button.

Aa140270.odc_beginner_macros_2(en-us,office.10).gif

Figure 2. The Stop Recording button

Running a Macro

You've recorded your first macro. Let's put it to work. Now every time you want to insert this table into your document, follow these easy steps:

  1. From the Tools menu, point to Macro, and click Macros. (You can also press ALT+F8 to access the Macros dialog box.)
  2. Select the macro that you want to run (InsertTable1 for me).
  3. Click Run.

You should see your table appear in your document wherever your insertion point is.

Assigning a Button to Your Macro

Because this is a table that you use often, you may want to make it even easier to insert it. Let's assign a toolbar button to this macro, so that inserting this table is only one click away. You can assign a button to your macro for use on any available toolbar. However, we will start from scratch and create a new toolbar.

To assign this macro to a toolbar button:

  1. On the Tools menu, click Customize.

  2. Click the Toolbars tab.

    Aa140270.odc_beginner_macros_3(en-us,office.10).gif

    Figure 3. The customize dialog box

  3. Click New.

  4. Type your toolbar name (I named mine InsertTable).

    Aa140270.odc_beginner_macros_4(en-us,office.10).gif

    Figure 4. The new toolbar

  5. Click OK.

    You should notice a new menu listed and checked. The check box indicates that this menu is in use. You can also see on your document there is a floating toolbar.

    Aa140270.odc_beginner_macros_5(en-us,office.10).gif

    Figure 5. The Customize dialog box with InsertTable toolbar

  6. Click the Commands tab. Click Macros in the Categories list.

  7. You will see your macro listed in the commands list as Normal.NewMacros.InsertTable1.

    **Note   **Because I stored my macro in the default Normal template, the prefix for my macro is Normal. If you stored yours in a different template your macro will be listed as TemplateName.NewMacros.InsertTable1.

    Aa140270.odc_beginner_macros_6(en-us,office.10).gif

    Figure 6. The new toolbar with a new button

  8. Drag this macro from the Commands list to your new toolbar.

    Aa140270.odc_beginner_macros_7(en-us,office.10).gif

    Figure 7. The new toolbar

  9. You will notice the new toolbar change its appearance. It should now look like this:

    Aa140270.odc_beginner_macros_8(en-us,office.10).gif

    Figure 8. The new toolbar with a new button

In my opinion, undocked toolbars clutter up my workspace, so I like to dock them with the rest of my menus at the edge of my workspace. To do this, simply drag this new menu where you would like to dock it.

Here's what my toolbars look like after I dock this toolbar:

Aa140270.odc_beginner_macros_9(en-us,office.10).gif

Figure 9. The new toolbar is docked to the edge of the workspace

Now, every time you click the Normal.NewMacros.InsertTable1 button, your table will be inserted into the document wherever your insertion point is.

Let's customize the appearance of our new toolbar button by changing the name and selecting an icon that will be displayed with the new toolbar button.

  1. On the Tools menu, click Customize.
  2. With the Customize dialog box open, do the following:
    • Click the Normal.NewMacros.InsertTable1 button on the toolbar.
    • In the Customize dialog box, click Modify Selection, click Name on the shortcut menu, and then type over the current name with a new name. I changed mine to InsertTable1.
    • Click the Normal.NewMacros.InsertTable1 button on the toolbar.
    • In the Customize dialog box, click Modify Selection, point to Change Button Image on the shortcut menu, and then click an image.

Assigning Your Macro to a Keyboard Shortcut

For an existing macro, like InsertTable1, you can easily assign keyboard shortcuts. Some Word users prefer these to reduce the use of the mouse.

  1. On the Tools menu, click Customize.

  2. With the Customize dialog box open, select the Commands tab and click the Keyboard button.

  3. Select Macros from the Categories list.

  4. Select your macro (InsertTable1).

    Aa140270.odc_beginner_macros_10(en-us,office.10).gif

    Figure 10. The Customize Keyboard dialog box

  5. In the Press new shortcut key box, type the keyboard shortcut that you want assigned to your macro. Notice that as you enter a keyboard shortcut, if the command is already used, its assignment appears below the Press new shortcut key box. Press CTRL+C for example, and notice that EditCopy appears. Assign your macro to ALT+4. You should notice that this is unused, and therefore displays [unassigned].

    **Note   **You will override the assigned shortcut key by assigning a macro to an already assigned key.

  6. Click Assign.

Now, every time you press ALT+4, your table will be inserted into your document wherever your insertion point is.

**Note   **You also have the option to set the keyboard shortcut using the Press new shortcut key box when you first record a macro.

What Does the Code Look Like?

To look at the code that was recorded:

  1. On the Tools menu, point to Macro, and click Macros.
  2. Click the macro whose code you want to view (for example, InsertTable1).
  3. Click Edit.

This will display the Microsoft Office Visual Basic® Editor. What you are seeing here is the code behind the steps, commands, and settings that were recorded to create your table. This is the code that runs when you run your macro. Your code may vary slightly from mine, depending on the exact keystrokes used to create your table. It's not important at this time to understand what this code means, but it helps complete the picture of what macros are.

**Note   **Recorded macros are stored in a module called NewMacros and they are included with the template in which they were recorded (in this case, Normal.dot).

Sub InsertTable1()
'
' InsertTable1
' Macro recorded 11/4/2001 by ErikOlson
'
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=10, 
      NumColumns _
        :=4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
    With Selection.Tables(1)
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
    End With
    Selection.TypeText Text:="Date"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="Check Number"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="Amount"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="Sent To"
    Selection.SelectRow
    Selection.Font.Bold = wdToggle
    With Selection.Cells
        With .Shading
            .Texture = wdTextureNone
            .ForegroundPatternColor = wdColorAutomatic
            .BackgroundPatternColor = wdColorGray15
        End With
        With .Borders(wdBorderLeft)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderRight)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderTop)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderBottom)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderVertical)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
        .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
        .Borders.Shadow = False
    End With
    With Options
        .DefaultBorderLineStyle = wdLineStyleSingle
        .DefaultBorderLineWidth = wdLineWidth050pt
        .DefaultBorderColor = wdColorAutomatic
    End With
End Sub

Conclusion

Using macros in Word is a great way to tap into the power of Word and get it to do some of your work for you. Recording macros is the first step in understanding how to use Word to automate your repetitive word processing tasks.