There may have been many instances where you have had to perform routine tasks repeatedly in a document. For example, while working on a project report you may need to write a biography of each individual that you interview on every page. You also need to affix the individual’s photograph and type the name in a particular style and position. You have to affix the photograph on the left margin and wrap the text to the right of the photograph. Imagine repeating the same task for the dozen or so individuals you want to profile! That’s probably why you’re reading this tutorial on creating Macros in Word.
Now, what if you could automate this task by simply clicking a button on the toolbar? The Macro tool allows you to do just that. With the help of a macro you can record your keystrokes and mouse operations as a series of commands to be played back at a later time. Just like a well-orchestrated symphony!
There are several advantages of using and creating Macros in Word:
- It automates repetitive and tedious tasks such as formatting.
- It enables you to work more efficiently and complete your work faster.
- If a macro is recorded correctly, it reduces the possibilities of errors in your documents.
Remember that there are over 900 commands available in Word, some of which are assigned to menus and toolbars. Therefore, you need to ensure that the macro you are creating is not already available. To view the list of available macro commands in Word:
- Click Tools on the menu bar.
- Select Macro and then Macros from the submenu. The Macros dialog box appears.
- Select Word commands in the Macros in drop down box. An alphabetical list of all commands is displayed in the Macro name box.
- Select a command name. On selecting a name, a description of thecommand appears in the Description box.
If the macro that you want to create is already listed, do not create another one. If you give a new macro the same name as an existing command, the actions of the new macro will replace the existing actions.
Creating Macros in Word
Once you have decided which macro you want to create, you need to prepare in advance before you actually begin its creation. You need to:
- Ensure that you have your steps memorized. This is important as the recorder will record each keystroke or mouse operation and include it in the macro.
- Ensure that you know the shortcut keys for the commands. This will help you save time while recording and creating a precise macro.
- Ensure that you use minimum steps to develop a leaner macro.
- Ensure that you carry out a test run of the macro before recording it.
You can create a macro in two ways:
- By recording steps
- By using Visual Basic Editor
Creating a Macro by Recording Steps
To create a macro by recording steps:
- Click Tools on the menu bar, point to Macro, and click Record New Macros. The Record Macro dialog box appears.
- Type a name for your macro in the Macro name box.Caution: While assigning a name to your macro, ensure that the name does not have any spaces or symbols. For example, you cannot name your macro as New Template.dot. The correct name would be NewTemplate.dot.
- Select the document or template in which you want to store your macro in the Store macro in box.Note: Word makes your macro available to all documents by default.
- Type a description for your macro in the Description box.
- Click Toolbars if you want to assign the macro to a toolbar or menu. The Customize dialog box appears.
- Click the Commands tab.
- Select the macro you are recording in the Commands box and drag it to the toolbar or menu you want to assign it to. Optionally, if you wish to assign the macro to shortcut keys, click Keyboard. The Customize Keyboard dialog box appears. In the Commands box, click the macro you are recording. In the Press new shortcut key box type the key sequence. Save the Macro in the desired file and then click Assign.
- Click Close to begin recording the macro. You will notice a Record Macro toolbar on the upper left corner of your screen and the mouse pointer will have a small icon that indicates that your actions are being recorded.
- Perform the actions you want to include in your macro. Press the Pausebutton if you wish to pause your recording.Note: While you can use the mouse to click commands and options, the macro will be unable to record mouse actions in a document window. Therefore, you need to use the shortcut keys for actions such as selecting, copying, or moving text.
- Click the Stop Recording button to stop recording your macro.
Creating a Macro Using VBA Editor
You can also create a macro by using a Visual Basic Applications (VBA) Editor. In fact, all macros are actually Visual Basic subroutines. A macro begins with the word Sub, the name of the macro, followed by parentheses, and ends with the word End Sub. Everything else in between is part of the code. For example:
Sub NewMacro ( ) Selection. TypeText Text:=”Word Processing” End Sub
Note: The VBA Editor helps you to create and edit macros without requiring any programming knowledge.
To create a macro using VBA Editor:
- Click Tools on the menu, point to macro, and click Macros. The Macro dialog box appears.
- Type a name for your macro in the Macro name box.
- Select the template or document in which you want to store the macro in the Macros in box.
- Click Create to open the VBA Editor. Alternately, click the Macro name and click Edit. The document that appears contains your macro.Note: Word stores all macros in templates. Any macro that you create is stored by default in Normal.dot, which is a global template. If you wish to store the macro in a specific document, save the macro in that particular document.
Caution: Save a copy of your template before beginning any editing of macros. A small error could render your macro useless.
Assigning Keyboard Shortcuts to a Macro
So, you have created a macro or macros to help you work faster on your documents and do away with repetitive tasks. However, in your quest to save time you actually end up creating half a dozen macros! You surely don’t want to waste time searching for these macros in the Macros dialog box, do you?
What can you do? You can assign a keyboard shortcut to your macro. That way, you can simply run your macro with the click of a mouse or by pressing a shortcut key.
To assign a shortcut key to a macro:
- Click Tools on the menu bar, and click Customize. The Customize dialog box appears
- Click Keyboard. The Customize Keyboard dialog box appears
- Scroll down the Categories box
- Select Macros
- Select the macro to which you want to assign the shortcut key in the Macros boxNote: If the macro has a keystroke assigned to it, the keystroke will appear in the Current keys box.
- Type the new shortcut key in the Press Shortcut key box if no shortcut key has been assigned to the macro or if you want to create a second shortcut key for your macro.
- Click Save changes in box to save your changes. If you wish to apply the changes to all Word documents select Normal from the drop down list. However, if you wish to use the shortcut key only in the current document, select the document name from the list.
- Click Assign, and then click Close.
Running a Macro
Once you have assigned a shortcut key to your macro, you are now ready to run the macro. To run a macro:
- Click Tools on the menu, point to Macro, and click Macros. The Macro Name box is displayed.
- Click the name of the macro you want to run.
- Click Run.
Alternately, you can also press the shortcut key to which the macro is assigned.
Setting the Macro Security Level
Sometimes you may be unable to copy macros from one document to another or transfer files that contain macros from one computer to another. One of the reasons why this may happen is that Word by default sets its macro security level to High to ward off macros with viruses or from suspicious sources. To change the macro security level:
- Click Tools on the menu bar, and click Options. The Options dialog box appears.
- Click the Security tab.
- Click Macro Security. The Security dialog box appears
- Click the Security Level tab
- Select the security level you want to use
Caution: Do not set the security level to Low because viruses travel fast at times and you may not catch them even with your anti-virus software. Therefore, set the security level to Medium where Word will prompt a warning before allowing you to use the macro.
Creating a Macro to Format Text
If you frequently need to work on a document that requires heavy formatting, create a macro. Creating a macro to format your document is quite simple:
- Select a section of the document where you want to apply the formatting.
- Turn on the macro recorder.
- Carry out the formatting tasks.
- Turn off the macro recorder.
- Select the text to which you wish to apply the formatting before you run your macro. The changes are applied to the selected portion only.
Run the macro. Any text that you now enter will retain the formatting.