Automating Word tasks with Macros

Updated 2015.11.17

Objectives

·        Learn how to create simple macros using the macro recorder.

Macros

Sometimes you may find yourself doing the same sequence of commands over and over and over. When this happens, you may wish that you could just press one key and the whole sequence of commands could be executed. This is what "macros" are for!

"Macro" is short for macro-instruction. "Macro" means "big", so a macro is a "big instruction". A macro is a collection of commands that is given a name and, optionally, assigned to a key on the keyboard (or a button on the ribbon). Then whenever you want to execute the collection of instructions, all you have to do is press the key that has been assigned to them (or click on the button on the ribbon), and the program will execute all of the instructions as if they were a single instruction.

To create a macro

All of the features for creating macros are on the Developer tab. If the Developer tab is not visible on your version of Word, do the following:

·        Click on the File tab.

·        Click on the Options button near the bottom of the menu. The Word Options dialog box will appear (below).

·        In the Categories pane (far left side), click Customize Ribbon.

·        In the list of Main Tabs (right side), click Developer to turn the check box on.

·        Click OK to close the Word Options dialog box.

To enable macros in Word

Word documents that have macros in them have the file extension .docm. If you try to open such a document, Word may not allow you to do so. If this happens, you will need to change a security setting:

1.      Click on the Developer tab.

2.      In the Code group, click on the Macro Security button.

3.      Click on the Enable All Macros radio button.

SNAGHTML17508408

4.      Click on the OK button.

5.      Close Word and re-open it again.

Example: a red text color macro

If you frequently change the font color, you may want to have a macro to do this for you. We will write a macro called RedText that will change the color of the selected text to red.

·        Select the text that you wish to change to red.

·        Click on the Developer tab.

·        In the Code group, click on the Record Macro button. The Record Macro dialog box will appear.

·        Macro Name: Enter RedText for the macro name. Macro names must be made up of letters and digits, must begin with a letter, and cannot have blanks in them.

·        Assign macro to: For this example, we will assign the macro to a key on the keyboard, so click on the Keyboard button. The Customize Keyboard dialog box will appear:

·        For the new shortcut key, type Ctrl+Shift+R.

·        Click the Assign button. Your shortcut key should jump from the Press new shortcut key box to the Current keys box.

·        Click on the Close button.

·        Every keystroke, mouse movement, menu selection, etc., that you make from now on will be "recorded" as part of the macro. However, Word seems to have a problem recording macros that change the font. To get font formatting commands to be saved as part of the macro, you must make your changes through the Font dialog box. Click on the Home tab.

·        Then click on the Font group's dialog box launcher:

·        The Font dialog box will appear:

·        Click on the Font tab.

·        In the Font dialog box, click on the Font color drop-down list box and select Red.

·        Click on the OK button.

·        This is the end of your macro.

·        To end your macro, click on the Developer tab. Then, in the Code group, click on Stop Recording.

 

Test your macro by selecting some text and typing Ctrl+Shift+R. The font for the selected text should change to Red.

Example: a Verdana macro

If you frequently change the font, you may want to have a macro to do this for you. We will write a macro called Verdana that will change the selected text to Verdana.

·        Select the text that you wish to change to Verdana.

·        Click on the Developer tab.

·        In the Code group, click on the Record Macro button. The Record Macro dialog box will appear.

·        Macro Name: Enter Verdana for the macro name. Macro names must be made up of letters and digits, must begin with a letter, and cannot have blanks in them.

·        Assign macro to: For this example, we will assign the macro to a key on the keyboard, so click on the Keyboard button. The Customize Keyboard dialog box will appear:

·        For the new shortcut key, type Ctrl+Shift+V (to make it different from Ctrl+v, which is the keyboard shortcut for paste).

·        Click the Assign button. Your shortcut key should jump from the Press new shortcut key box to the Current keys box.

·        Click on the Close button.

·        Again, we will have to go to the Font dialog box to record our commands. Click on the Home tab.

·        Then click on the Font group's dialog box launcher:

·        The Font dialog box will appear:

·        Click on the Font tab.

·        In the Font dialog box, click on the Font list box and select Verdana.

·        Click on the OK button.

·        This is the end of your macro.

·        To end your macro, click on the Developer tab. Then, in the Code group, click on Stop Recording.

 

Test your macro by selecting some text and typing Ctrl+Shift+V. The font for the selected text should change to Verdana.

Example: A big, red, bold, italic macro

·        Select the text that you wish to change to make big, red, bold, and italic.

·        Click on the Developer tab.

·        In the Code group, click on the Record Macro button.

·        Enter BigRed for the macro name.

·        We will also assign this to the keyboard. Click on the Keyboard button.

·        Click on the OK button.

·        For the new shortcut key, type Ctrl+Shift+B (for "Big"). Note that Ctrl+b (lower-case "b") is still the "bold" command.

·        Click the Assign button. Your shortcut key should jump from the Press new shortcut key box to the Current keys box.

·        Click on the Close button.

·        Everything you do from this point on will be part of your macro.

·        Click on the Home tab.

·        Then click on the Font group's dialog box launcher:

·        In the Font Style list box, click on Bold Italic.

·        In Size list box, click on 16.

·        In the Font Color click on Red from the Standard Colors row.

·        Click on the OK button.

·        This is the end of your macro.

·        To end your macro, click on the Developer tab. Then, in the Code group, click on Stop Recording.

 

Test your macro by selecting some text and typing Ctrl+Shift+B. The font for the selected text should change to 16-point bold italic red text.