Document.BeforeClose Event


Occurs immediately before the document closes.

event System.ComponentModel.CancelEventHandler BeforeClose;


The following code example displays a message box before the document closes. This example is for an application-level add-in.

private void DocumentBeforeClose()
    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.BeforeClose += new System.ComponentModel.CancelEventHandler(ThisDocument_BeforeClose);

void ThisDocument_BeforeClose(object sender, System.ComponentModel.CancelEventArgs e)
    System.Windows.Forms.MessageBox.Show("The document is closing.");
Private Sub DocumentBeforeClose()
    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    AddHandler vstoDoc.BeforeClose, AddressOf ThisDocument_BeforeClose
End Sub

Private Sub ThisDocument_BeforeClose(ByVal sender As Object, _
    ByVal e As System.ComponentModel.CancelEventArgs)
    System.Windows.Forms.MessageBox.Show("The document is closing.")
End Sub


The event occurs before the document closes. To keep the document from closing, set the Cancel argument of the provided CancelEventArgs object to true.

Applies to