[Tutorial] How to Create a Macro in MS Word 2007

ms-word-macro

One of the hidden features in Microsoft Word is macros. These are small mini-programs that you can create to automate tasks, for example, formatting a document, changing the layout or updating styles.

What is a macro? A macro is a series of commands and instructions that you group together as a single command to accomplish a task automatically.

Why create a macro?

It makes your life that much easier. You can setup a macro to:

  • Automate a complex series of tasks
  • Combine multiple commands — for example, to insert a table with a specific size and borders, and with a specific number of rows and columns
  • Make an option in a dialog box more accessible
  • Speed up editing and document formatting

To do this, you can use the macro recorder in Word to record a sequence of actions, or you can create a macro from scratch by entering Visual Basic for Applications. The Visual Basic Editor contains a complete debugging toolset for finding syntax, run-time, and logic problems in your code.

If it sound’s intimidating, don’t be alarmed. If I can do this, you can.

How to create macros in Word 2007

To work with macros in Office Word 2007, you need to have the Developer tab displayed. This is turned off by default.

Here is how you turn it on.

To show the Developer tab in Word 2007:

1. Click the Microsoft Office Button (top left corner in Word) and then click Word Options.

How to Create a Marco in Microsoft Word 2003/2007 - Part 1 by you.

2. Click Popular.

3. Select the Show Developer tab in the Ribbon check box.

How to Create a Marco in Microsoft Word 2003/2007 - Part 1 by you.

The Developer tab is now displayed to the far right of the ribbon.

How to record a macro

1. Click on the Developer tab, then in the Code area, click Record Macro.

How to Create a Marco in Microsoft Word 2003/2007 - Part 1 by you.

2. In the Macro name box, type a name for the macro.

How to Create a Marco in Microsoft Word 2003/2007 - Part 1 by you.

Careful! If you name the new macro the same as a built-in macro in Word 2007, the new macro actions will replace the built-in macro.

3. In the Store macro in box, click the template or document where you want to store the macro.

Store Marco in template or document where you want to store the macro.

Maybe you want to apply the macro to one document only or maybe to all documents. It’s up to you.

4. In the Description box, type a description, for example, Change Fonts to Verdana.

5. Do one of the following:

To save the macro — without assigning it to a button on the Quick Access Toolbar or to the keyboard — click OK.

Otherwise…

To assign the macro to the Quick Access Toolbar:

1. Click the Button icon.

To assign the macro to the Quick Access Toolbar

2. Under Customize Quick Access Toolbar, select the document (or all documents) you want to add the macro to the Quick Access Toolbar.

3. Under Choose commands from dialog box, click the macro you’re recording, and click Add.

4. Click OK to begin recording the macro.

To assign the macro to the Keyboard

1. Click Keyboard.

2. In the Commands box, click the macro that you are recording.

To assign the macro to the keyboard

3. In the Press new shortcut key box, type the key sequence you want (e.g. A + F4) , and then click Assign.

4. Click Close to begin recording the macro.

5. Perform the actions that you want to include in the macro.

6. When finished recording, click Stop Recording in the Code group.

To stop recording the macro

How to use Visual Basic for Applications

If you need to update the macro, you can go directly into the code and change it through the Visual Basic for Applications editor.

How to use Visual Basic for Applications to modify or update your macro

Note that this is not the Visual Basic software that developers use to create applications but is an editor —that is already inside Microsoft Word — which uses a scripting language to update the macro.

If you’re familiar with any scripting language, then you’ll soon figure this out.

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

2. In the Macro name box, type a name for the macro.

Tip: If you give a new macro the same name as a built-in macro in Office Word 2007, the new macro actions will replace the built-in macro. To view a list of built-in macros, click Word Commands in the Macros in list.

3. In the Macros in list, click the template or document in which you want to store the macro.

4. Click Create to open the Visual Basic Editor.

How do I run the macro in Word 2007?

You have a few choices.

If you created the macro to run from the menu bar, click the Button you added there.

If you created the macro to run from your keyboard, click the numbers
and/or letters you assigned
to the macro, for example, I use F4 to run a formatting macro and F5 to run a macro that changes all the text to US English.

If you setup neither of these, you can run the macro as follows:

1. Click on the Developer tab.

2. Click Macros.

3. Select your macro.

4. Click Run.

you can run the macro as follows

This activates the macro. It runs through the actions as per your instructions.

How to create a macro in Word 97/2000/2003

Open a new Word document, and type a few words.

1. Click Tools.

2. Scroll to Macro, and click Record New Macro.

3. Type Test in the Macro Name: window and click OK.

An activity box appears which shows that you are now in Record mode. You can use shortcut keys for the commands you want or you can use your mouse.

4. Select one of the words that you typed before starting the macro.

5. Now (with the word still highlighted) change the font style (example: “Times New Roman” to “Verdana”) and the font size.

6. Remove the highlight from the word by pressing your right-arrow key.

7. Click the square in the CommandBar to close it and stop the Macro recording.

8. Click Tools, Macro, Macros, click Test, and then click Run.

The commands you made while recording your macro will take place when you run the macro!

9. Next, click Tools, Macro, Macros, Test, Edit, File, and then click Save Normal.

You’ve now saved the macro and added it to the list of macros in Word.

Finally, close the Microsoft Visual Basic editor and assign a shortcut key to your new macro, for example, if you want to use it in the future. This is optional. You don’t have to. When you re-open the Macro Editor, you’ll see that the Test macro is still there.

How to test your new macro?

1. Using the example above, click to the right of any word in a document and then run your macro.

2. The font and font size will change automatically, e.g. it will change from Times Roman to Verdana.

You can use the macro recorder to record a sequence of actions, or you can create a macro from scratch by entering Visual Basic for Applications code in the Visual Basic Editor.

My macro doesn’t work?

There could be a few reasons for this.

1. Check if the macro is setup to run in all active templates.

You might have setup the macro to run in the current document only or maybe in the
Normal.Dot file only. Open the macro, check this setting and then re-run.

2. When you record a macro, you MUST use the mouse to click commands and options on Word’s menu bars. You can’t select text.

You MUST use the keyboard to select text.

3. You didn’t assign the macro to a button or to the keyboard.

If you didn’t do this, then you need to go into the Developer tab (far right on Word menu bar
in 2007), click Macros and then select the macro from the list.

How to Write Word Macros

Want to learn more?

You MUST read this book on Mastering VBA for Microsoft Office 2007.

Also, try this here and this here and here.

Did your macro work?

I hope so!

If not, let me know what went wrong and we’ll try to fix it.

PS: Where are the built-in macros in Word 2007? To see a list of built-in macros, click on the Developer tab, then Code group, Macros, and Word Commands.

Reblog this post [with Zemanta]

Have you seen these templates?