Document.BeforeSave Event

Occurs before the document is saved.

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 BeforeSave As SaveEventHandler
event SaveEventHandler BeforeSave

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

Private Sub DocumentBeforeSave()
        Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
        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 = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    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 Interface

Microsoft.Office.Tools.Word Namespace