Document.BeforeClose Event (2007 System)

Occurs immediately before the document closes.

Namespace:  Microsoft.Office.Tools.Word
Assembly:  Microsoft.Office.Tools.Word.v9.0 (in Microsoft.Office.Tools.Word.v9.0.dll)

Syntax

'Declaration
Public Event BeforeClose As CancelEventHandler
'Usage
Dim instance As Document 
Dim handler As CancelEventHandler 

AddHandler instance.BeforeClose, handler
public event CancelEventHandler BeforeClose
public:
 event CancelEventHandler^ BeforeClose {
    void add (CancelEventHandler^ value);
    void remove (CancelEventHandler^ value);
}
JScript does not support events.

Remarks

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

Examples

The following code example displays a message box before the document closes.

Private Sub DocumentBeforeClose()
    AddHandler Me.BeforeClose, AddressOf ThisDocument_BeforeClose
End Sub 

Private Sub ThisDocument_BeforeClose(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
    MessageBox.Show("The document is closing.")
End Sub
private void DocumentBeforeClose()
{
    this.BeforeClose += new System.ComponentModel.CancelEventHandler(ThisDocument_BeforeClose);
}

void ThisDocument_BeforeClose(object sender, System.ComponentModel.CancelEventArgs e)
{
    MessageBox.Show("The document is closing.");
}

This version is for an application-level add-in.

Private Sub DocumentBeforeClose()
    Dim vstoDoc As Document = Me.Application.ActiveDocument.GetVstoObject()
    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
private void DocumentBeforeClose()
{
    Document vstoDoc = this.Application.ActiveDocument.GetVstoObject();
    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.");
}

.NET Framework Security

See Also

Reference

Document Class

Document Members

Microsoft.Office.Tools.Word Namespace

Change History

Date

History

Reason

July 2008

Added a version of the code example for an application-level add-in.

SP1 feature change.