What to Do with Macro Code in Microsoft Word


Somebody gave you some code to use, but you don't know what to do with it? This should help, though you may require more information than they gave you. These are general instructions and won't likely work for everyone under every circumstance.

For All Documents

If you want the code to be able to run in any document, then it must be stored in a global template. Some people will tell you not to use normal.dot, which is the default global template. However, I think it's fine to use it, but you should read about normal.dot, understand it, and protect a copy of it.

Open Word. Hit Alt+F11 or Tools Macro Visual Basic Editor (VBE).

If you don't see it as shown in the screen capture above, you may need to choose from the menu, View Project Explorer, and View Code.

On the left, select Normal.

From the menu, choose Insert Module. The new module appears and a code window appears at right.

Paste the code you were given in the code window at right.

Hit the Save diskette on the toolbar. This saves normal.dot regardless of any other settings. Click here to learn how to assign the macro to a toolbar button or shortcut key.

For a Single Document

Use the same instructions as provided For All Documents, except instead of choose Normal from the Project Explorer window, you must open your document, go to the VBE and choose Project (Your Document). You'll still insert a module, etc.

Run When the Document Opens or Is Newly Created

Whether you use it for all documents (place the code in Normal) or only for a specific document (place in the single document), you can run code when a document opens or when a document is newly created.

Double-click the document. We have chosen Document1. Then, from the drop-down at the top of the code window, choose Document.

A new Document_New procedure is created for you, but no code is provided. Document_New will only work if your document is a template. The Document_New event is fired when a new document is created from a template. Change "New" to "Open" to have it work when the document is opened.

You may need to take the code provided, remove the first and last lines, and paste the remaining code between the Private Sub... and End Sub lines in the code window.

Don't forget to hit the Save diskette.