Share via


WorkbookBase.BeforeSave Event

Definition

Occurs before the workbook is saved.

public:
 event Microsoft::Office::Interop::Excel::WorkbookEvents_BeforeSaveEventHandler ^ BeforeSave;
public event Microsoft.Office.Interop.Excel.WorkbookEvents_BeforeSaveEventHandler BeforeSave;
member this.BeforeSave : Microsoft.Office.Interop.Excel.WorkbookEvents_BeforeSaveEventHandler 
Public Custom Event BeforeSave As WorkbookEvents_BeforeSaveEventHandler 

Event Type

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.

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

Applies to