Freigeben über


WorkbookBase.BeforeSave Event

Occurs before the workbook is saved.

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

Syntax

'Declaration
Public Event BeforeSave As WorkbookEvents_BeforeSaveEventHandler
public event WorkbookEvents_BeforeSaveEventHandler BeforeSave

Examples

The following code example demonstrates a handler for the BeforeSave event. The event handler prompts the user to either continue with the save operation or cancel it. If the user cancels the save operation, then the Cancel parameter of the WorkbookEvents_BeforeSaveEventHandler event handler is set to true so that Microsoft Office Excel does not save the workbook.

This example is for a document-level customization.

Sub ThisWorkbook_BeforeSave(ByVal SaveAsUI As Boolean, _
    ByRef Cancel As Boolean) Handles Me.BeforeSave

    If DialogResult.No = MessageBox.Show("Are you sure " & _
        "you want to save the workbook?", "Sample", _
        MessageBoxButtons.YesNo) Then
        Cancel = True
        MessageBox.Show("Save is canceled.")
    End If 
End Sub
private void WorkbookBeforeSave()
{
    this.BeforeSave +=
        new Excel.WorkbookEvents_BeforeSaveEventHandler(
        ThisWorkbook_BeforeSave);
}

void ThisWorkbook_BeforeSave(bool SaveAsUI, ref bool Cancel)
{
    if (DialogResult.No == MessageBox.Show("Are you sure you want to " +
        "save the workbook?", "Example", MessageBoxButtons.YesNo))
    {
        Cancel = true;
        MessageBox.Show("Save is canceled.");
    }
}

.NET Framework Security

See Also

Reference

WorkbookBase Class

Microsoft.Office.Tools.Excel Namespace