Macros in Mac Word

Contents

  1. How to Install a Macro
  2. Useful Articles About Macros
  3. An Introduction to the Visual Basic Editor in MacWord

How to Install a Macro

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:

Sub MyMacroName()
'
' MyMacroName Macro
' Macro created 03/08/05 by Me
'
[cursor will be here]
End Sub

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.

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, see 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:

  1. In your fixing of the code, you have damaged one of the statements.
  2. The macro you have been sent was created for WinWord and requires some changes to run on a Mac.
  3. 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.

Back to Top

More Useful Articles About Macros

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 Refresh a few times, or use a different browser.

Basics:

Creating a macro with no programming experience using the recorder

How to assign a macro to a toolbar

How to assign a macro to a keyboard shortcut

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.

Slightly More Advanced:

How to modify a recorded macro

Running a macro automatically when Word starts or quits

Running a macro automatically when a document is created, opened or closed

Back to Top

An Introduction to the Visual Basic Editor (VBE)

The Visual Basic Editor (VBE) is the environment where one can write and edit macros. Macros are written in Visual Basic for Applications (VBA), one of a number of coding languages.

Macros are saved inside templates. Within templates, macros are saved in Modules, which hold collections of macros. By default, macros you create go into the NewMacros module in the Normal template. However, you can create other modules within the Normal template, and you can save your macros in other templates if you prefer.

To experiment with modules and macros, you will need to explore the VBE (Visual Basic Editor). You can use simple macros perfectly happily without knowing any of the information that follows. However, this section may help you understand Word a little better, giving you more power over what you do with it. Macros are designed to help you automate Word to make your life easier.

Before You Begin

There is limited undo ability, and no backup here; Word assumes that if you are in this deep you know what you are doing. It would be a good idea to make a copy of your Normal template before you begin playing with macros. Then afterwards you can dump the copy with the macro experimentation and swap back in the older Normal template. For more about the Normal template and how to find it, see here.

It is possible to test macros from the VBE directly. You can run the macro while you are still in the VBE by pressing F5. Make sure the cursor is in the macro you want to test. You can also size the VBE and the Word window on your screen so you can see them both at once, and step through the macro one line at a time, in order to see what it does. Press F8 to begin and to move to each line. (Of course, function keys on a laptop may not behave. F5 is Run>Run Sub/UserForm. F8 is Debug>Step Into.)

If you are going to use Macros a lot, you can use Tools>Customize to customize the keyboard, menus and toolbars to make switching in and out of the VBE easier. You can also switch between the VBE and Word, and arrange windows to see both at once; you do not need to close the VBE to access Word.

Understanding the VBE

  1. Start Word and open your Visual Basic Editor from Tools>Macro>Visual Basic Editor. Try not to get thrown by the fact that this puts you straight into an unfamiliar environment; this is a lump of WinWord code that was converted to Mac with as little work as possible to keep the price of Word down. It works — don't expect it to be nice to use.
  2. You will see a Pane on the top left named Projects. Keep looking until you find it; no other window will do. Users can undock these windows and move them, so be prepared for the fact that they may not be where they usually are.
  3. At the top of this window you should see an entry named Normal. This is the programmer's eye view of your Normal template. Any global templates that are loaded will also show up in this list, but you cannot manipulate the macros in other templates unless the template is open in Word. The VBE sees each template as a “Project”, and Modules as the items within the Project (hence the Organizer lists modules under Macro Project Items).
  4. Click the arrow to the left of Normal to expand the tree. You should see a folder named Modules. If you do not see it, the template does not yet contain any macros.
  5. Select the Normal entry and choose Insert>Module. You must select Normal or Word will add the module to the wrong project. If there was no Modules folder, there will be now.
  6. Below the Projects window you should now see a Properties window. If you can't, use View>Properties Window to bring it up.
  7. In the Properties window be sure the Alphabetic tab is the active one. You should see a single item in here: (Name) Module1. Select the name Module1 and type a new name over the top of it. You can call it anything you like so long as the name contains no spaces, for instance, “MyMacros”. Do not call it "NewMacros"; that is the name Word uses for the place where it saves recorded macros. If you call your folder the same thing, there is a severe danger that the next time you record a macro you will overwrite the one you are installing now.
  8. Each module will open in its own window in the VBE. At the top right of each window will be a dropdown menu that lists the macros in that module and lets you navigate among them. You can type a Sub MacroName statement directly into the window to create a macro.

You may be interested in a more sophisticated discussion of the VBE here. The page was written for Excel, but the general understanding is similiar.

Organizing Macros

You can transfer modules to a different open template using Tools>Macro>Macros>Organizer. To transfer one single macro, you will need to go into the VBE, and cut and paste the text of the macro to a different template. Or you can create a module that only holds one macro, and use the Organizer to transfer the module. To create a macro in a different template, you have to use the VBE directly, not Tools>Macro>Macros.

Once you get a collection of macros, it’s good practice to move them out of the NewMacros module. That’s because NewMacros is where Word puts macros you record: it’s good to avoid the possibility that you might inadvertently overwrite one of your valuable macros. The simplest way to move NewMacros is to re-name the NewMacros module to something else. Be aware that when you do this, you will have to re-assign your macros to your toolbars or keystrokes, since the full name of the macro includes the name of the template and module that contains it.

© 2005 • http://daiya.mvps.org/installMacroMac.htm • daiya at-symbol mvps period-symbol org•

Return to TopReturn to IndexWhat's An MVP?