This documentation is archived and is not being maintained.

Using Events with the Document Object

Office 2007

The Document object supports seven events: BeforeClose , Open , Redo , ShapesAdded , ShapesRemoved , Undo , and WizardAfterChange . You write procedures to respond to these events in the class module named "ThisDocument." Use the following steps to create an event procedure.

  1. Under your publication project in the Project Explorer window, double-click ThisDocument. (In Folder view, ThisDocument is located in the Microsoft Publisher Objects folder.)
  2. Select Document from the Object drop-down list box.
  3. Select an event from the Procedure drop-down list box.

    An empty subroutine is added to the class module.

  4. Add the Visual Basic instructions you want to run when the event occurs.


This example shows an Open event procedure that displays a message when a publication is opened.

Private Sub Document_Open()
    MsgBox "This publication is copyrighted."
End Sub

The following example shows a BeforeClose event procedure that prompts the user for a yes or no response before closing a document.

Private Sub Document_BeforeClose(Cancel As Boolean)
    Dim intResponse As Integer

    intResponse = MsgBox("Do you really want to close " _
        & "the document?", vbYesNo)

    If intResponse = vbNo Then Cancel = True
End Sub
Aa437527.vs_note(en-us,office.12).gif  Note
For information on creating event procedures for the Application object, see Using Events with the Application Object .