Aracılığıyla paylaş


Document.BeforeSave Event (2007 System)

Occurs before the document is saved.

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

Syntax

'Declaration
Public Event BeforeSave As SaveEventHandler
'Usage
Dim instance As Document 
Dim handler As SaveEventHandler 

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

Remarks

To prevent the document from being saved, set the Cancel argument of the provided CancelEventArgs object to true.

Examples

The following code example displays a message before the document is saved that asks whether you want to save the document.

This version is for a document-level customization.

Private Sub DocumentBeforeSave()
    AddHandler Me.BeforeSave, AddressOf ThisDocument_BeforeSave
End Sub 

Private Sub ThisDocument_BeforeSave(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Word.SaveEventArgs)
    If MessageBox.Show("Do you want to save the document?", "BeforeSave", _
        MessageBoxButtons.YesNo) = DialogResult.No Then
        e.Cancel = True 
    End If 
End Sub
private void DocumentBeforeSave()
{
    this.BeforeSave += new Microsoft.Office.Tools.Word.SaveEventHandler(ThisDocument_BeforeSave);
}

void ThisDocument_BeforeSave(object sender, Microsoft.Office.Tools.Word.SaveEventArgs e)
{
    if (MessageBox.Show("Do you want to save the document?", "BeforeSave",
        MessageBoxButtons.YesNo) == DialogResult.No)
    {
        e.Cancel = true;
    }
}

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

Private Sub DocumentBeforeSave()
    Dim vstoDoc As Document = Me.Application.ActiveDocument.GetVstoObject()
    AddHandler vstoDoc.BeforeSave, AddressOf ThisDocument_BeforeSave
End Sub 

Private Sub ThisDocument_BeforeSave(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Word.SaveEventArgs)
    If System.Windows.Forms.MessageBox.Show( _
        "Do you want to save the document?", "BeforeSave", _
        System.Windows.Forms.MessageBoxButtons.YesNo) = System.Windows.Forms.DialogResult.No Then
        e.Cancel = True 
    End If 
End Sub
private void DocumentBeforeSave()
{
    Document vstoDoc = this.Application.ActiveDocument.GetVstoObject();
    vstoDoc.BeforeSave += new Microsoft.Office.Tools.Word.SaveEventHandler(ThisDocument_BeforeSave);
}

void ThisDocument_BeforeSave(object sender, Microsoft.Office.Tools.Word.SaveEventArgs e)
{
    if (System.Windows.Forms.MessageBox.Show("Do you want to save the document?", "BeforeSave",
        System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
    {
        e.Cancel = true;
    }
}

.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.