共用方式為


Document.BeforeClose Event

Occurs immediately before the document closes.

Namespace:  Microsoft.Office.Tools.Word
Assemblies:   Microsoft.Office.Tools.Word.v4.0.Utilities (in Microsoft.Office.Tools.Word.v4.0.Utilities.dll)
  Microsoft.Office.Tools.Word (in Microsoft.Office.Tools.Word.dll)

Syntax

'Declaration
Event BeforeClose As CancelEventHandler
event CancelEventHandler BeforeClose

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. This example is for an application-level add-in.

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
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.");
}

.NET Framework Security

See Also

Reference

Document Interface

Microsoft.Office.Tools.Word Namespace