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

Article contributed by Dave Rado

Using Document events

Open your template, press Alt+F11, and in  the Project window of the VBA environment, double-click on  Microsoft Word Objects, then on ThisDocument. On the toolbar you'll see two list boxes. If you pull down the one on the left, and change it from (General) to Document, a procedure called Document_New() will be created. If you then pull down the list box on the right, you'll see three events to choose from: Close, New and Open. You can select Close or Open from the list to insert a Document_Close() or Document_Open() procedure (or you can forget about the list boxes and just type, once you know the syntax).

A Document_New() procedure will run when a document based on that template is created; a Document_Open() procedure will run whenever a document based on that template is opened; and Document_Close() will run a document based on that template is closed.

Note that these procedures cannot be made global – they will only be fired when documents based on the  template are created or opened or closed.
  

Using Auto macros

Another way of achieving the same objective is to create a Module (Insert + Module), and write a macro called AutoNew(), AutoOpen(), or AutoClose(). If stored in any template other than Normal.dot, these will behave in the same way as Document events; i.e. they will be fired when documents attached to the template are created, opened or closed.

However, if stored in Normal.dot, they will act globally – in other words, they will be fired when any document is created, opened, or closed. This is in contrast with a Document_Open procedure stored in Normal.dot, which will only execute when documents based on Normal.dot are opened.

Unfortunately, AutoNew, AutoOpen and AutoClose macros stored in an Addin (a .dot file stored in Word's Startup directory) will not behave globally. In fact there is no point in storing AutoNew, AutoOpen or AutoClose macros in an Addin, because you would (or should) never base a document on an Addin.
  

Using Application Events

If you want a macro to be fired whenever any document is opened, regardless of which template the document is attached to, the simplest way, as discussed above is to write an AutoOpen macro and store it in Normal.dot. However there are problems associated with storing macros in Normal.dot, so if you want to avoid that route, the answer is to use Application Events. Application Events stored in global Addins do behave globally. And rather confusingly, some application events relate to documents.

In Word 97, you can use the DocumentChange event of the Application object to simulate global Auto macros; and in Word 2000, you can use the DocumentOpen, NewDocument and DocumentBeforeClose events of the application object. Storing these in an Addin works just like storing Auto macros in Normal.dot (i.e. they're global).

See the article: How to create global event procedures similar to AutoOpen, AutoNew and AutoClose, without using Normal.dot for more details.