Install a Macro in MacWord
Applies to all versions before Word 2008. Word 2008 does not support VBA or macros.
If someone on a newsgroup gave you a macro or some VBA to fix a problem, this is what you do with that text. Macros in Word are written in a coding language called VBA, or Visual Basic for Applications. This article explains how to install a macro step by step. It assumes no prior knowledge, but does presume that you have the text of the macro ready to copy.
Go to Tools>Macro>Macros…. You will get a dialog box that lists all existing macros, if you have any. In the box for Macro Name, type a name for your macro—the name must not contain spaces. Click Create.
Naming Macros: It is conventional to express VBA names with each word capitalized and the spaces taken out. For example: "ToggleWebToolbar." If you do use capitalization as suggested, Word will expand the name into a tooltip (yellow balloon) when you place a button for the macro on the toolbar. For example, the tooltip will read "Toggle Web Toolbar."
After clicking Create, Word will dump you into the Visual Basic Editor (VBE, the environment where one can write and edit macros written in VBA). It may look very strange to you. Basically, it’s a three-pane layout, and the code you are about to work on should be in the right-hand (largest) pane.
You will see something like:
' MyMacroName Macro
' Macro created 03/08/05 by Me
[cursor will be here]
You can paste the provided macro code right where the cursor is.
Lines that begin with an apostrophe ' are comments. When you run the macro, the comment lines are ignored. All macros created by Word include these two lines of information. It’s a very good idea to update them or add more information so that in a few years time you will know where this macro came from and what it was supposed to do.
In most cases, the code you have been given will paste without any problems or any red text, and you can test it. However, you may need to make some trivial fixes.
Common Problems: All macros must start with a Sub statement and end with an End Sub. If the code someone gave you already had the Sub/End Sub in it, you will need to delete the extra Sub lines. It is conceivable that a sophisticated piece of code may also contain Function() and End Function statements: for the purposes of this article, treat them as equivalent.
Lines that show up in red have errors. This is most likely because there are line breaks in the wrong place, since newsgroups and email can force shorter line breaks. To fix these, place your cursor at the end of the first red line, and start tapping Delete. When you delete all the invisible spaces and carriage returns, the next line will move up and that may fix the problem. Do not delete any visible characters. Be aware that the statement in red may continue over three or four lines. You may need to re-enter a space that you delete.
Laptop users: the delete key usually behaves as a backspace key. You could also backspace from the beginning of the lines in red, to let the VBE wrap the lines naturally.
Also, if you copied the macro from a web browser and pasted directly into the VBA editor, you are likely to run into the "non-breaking-space bug." In the web browser, all the spaces were transposed into non-breaking spaces to preserve the layout, because HTML rules crunch multiple spaces into one. The cure is to replace all the spaces in front of the red lines with "real" spaces, and the problem will go away. If you have to do a lot of them, paste the text first into a blank Word document, then use Find/Replace to replace the non-breaking spaces with ordinary spaces.
To Test the Macro: in the VBE, click on the W button or use Word>Close And Return To Microsoft Word to go back to Word. Go to Tools>Macro>Macros…, select the macro name in the list, and click Run.
If you expect to run the macro regularly, you can assign it to a keyboard shortcut, toolbar, or menu. See Useful Articles for explanations on how to do so. To learn more about the Visual Basic Editor and organizing Macros, click here.
More Complex Errors:
If the test run fails, return to Tools>Macro>Macros…, select the name of your macro, and click Edit. This will take you back to the Visual Basic Editor (VBE) and show you the code of your macro. Go to the Debug menu and choose Compile Normal (by default, the macro will have been saved in the Normal template).
It should appear as though nothing happened. If there is still a problem, Word will pop up an error and highlight the statement that contains it in yellow. You can look up the error messages in the VBA Help. However, for the purposes of this article, we can assume that there are only three causes:
- In your fixing of the code, you have damaged one of the statements.
- The macro you have been sent was created for WinWord and requires some changes to run on a Mac.
- The macro you have been sent is incomplete and never did work, even for the author!
Whatever the cause, unless you know VBA well, it will be much quicker to go back to the newsgroup or source of the code and ask. Suggest that the author send you the code again, and this time formats his/her message in HTML. HTML encoding is frowned upon in newsgroups, but it prevents these problems with broken line endings.
If the author of the code has created it in Windows, post the text of the code into the MacWord newsgroup and ask; the more sophisticated the macro, the more likely it is that it will require changes to run in MacWord. The person using Windows will not be able to help, because it is very difficult for even an experienced coder to tell what will work on the Mac and what won’t: the only reliable way is to compile the macro in MacWord and try it. In general, anything that will run in WinWord 97 will work on the Mac; anything that requires VBA6 will require tricky workarounds on the Mac.
If you suspect number 3, you might delicately ask if the author tested the code before sending it to you. Unfortunately, the Internet being what it is, there are people out there who for some reason sometimes send you code they haven’t tested that doesn’t work.
These articles were written with WinWord in mind, but all will work for MacWord. The exact location of menus might be a little different, but the terms will be the same and changes should be self-evident. Some of the articles give keyboard shortcuts to open dialogs, however, which will not work on the Mac; you will have to use the menus. If you have problems, post the article link and the exact text that confuses you in the MacWord newsgroup (microsoft.public.mac.office.word, if you are already familiar with newsgroups).
Attention Safari Users: These links will not work immediately—you will need to hit Reload a few times, or use a different browser.
Understand that a macro and how you access it are separate items. When you record a macro, Word offers you the chance to assign it to a toolbar or keyboard shortcut. Nevertheless, that toolbar/keyboard assignment is not part of the macro. It is a separate setting in Word, layered on top of the macro. If you send the text of a macro to a friend, for instance, the toolbar/keyboard assignment will not travel with it.