Share via


DocumentBase.BeforeSave Event

Occurs before the document is saved.

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

Syntax

'Declaration
Public Event BeforeSave As SaveEventHandler
public 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. To use this example, run it from the ThisDocument class in a document-level project.

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;
    }
}

.NET Framework Security

See Also

Reference

DocumentBase Class

Microsoft.Office.Tools.Word Namespace