Software

How to automatically execute a Word macro when you create, open, or close a document

By adding a macro to a template's New, Open, and Close event procedures, you can automate a number of tasks, making you more efficient and productive.

You probably know that you can reduce the amount of time you spend formatting documents by making those format changes to your template, but did you know that you can add macros to a template? You'd do so to automate a regular task in all documents based on the template. Specifically, you can add macros to a template that run when you create a new document or open and close an existing document (based on the template).

Automate a task when creating a new document To create a new document, you click New, press [Ctrl]+N, or choose New from the File menu/tab. You can get Word to execute a task when you create a new document using the Document_New event procedure, as follows:

  1. Open the template. You'll find normal.dot or normal.dotm in the Documents and Settings\Administrator/user\Application Data\Microsoft\Templates folder. You can use any template, not just normal.dot or normal.dotm.
  2. Launch the Visual Basic Editor (VBE) by pressing [Alt]+F11.
  3. In the Project Explorer, double-click ThisDocument.
  4. In the resulting module, enter the event procedure shown below.
  5. Click Save and close the VBE.

Private Sub Document_New()

  'Greet user.

  MsgBox "Greetings", vbOKOnly, "Greetings"

End Sub

When you return to Word, close the template file. Then, create a new document. Word will open a new blank document and display a simple greeting in a message box. Click OK to close the message. (The macro is simple on purpose as this technique is about the event procedure and its relationship to a template. What the macro does isn't important to the technique.) Automate a task when opening an existing document You can automate a task when opening an existing document in much the same way. The only thing that changes is the event procedure. In this case, you'd use the following Document_Open event:
Private Sub Document_Open()

  'Greet user.

  MsgBox "Greetings", vbOKOnly, "Greetings"

End Sub

You can apply this macro to a template or to an existing document. If you add this macro to the template, Word will save it with every new document you create. In other words, every document you create (based on that template) will execute this macro every time you open it.

Automate a task when closing an existing document I have one last event procedure to cover - one that automates a task when you close a document. Like the last macro, if you add the macro to a template, Word will execute the macro every time you  close any document based on the template. Use the following event procedure:
Private Sub Document_Close()

  'Greet user.

  MsgBox "Greetings", vbOKOnly, "Greetings"

End Sub

Automating a task for all documents by adding it to the template could make you more efficient!

About

Susan Sales Harkins is an IT consultant, specializing in desktop solutions. Previously, she was editor in chief for The Cobb Group, the world's largest publisher of technical journals.

Editor's Picks