Bagikan melalui


Workbook.BeforeSave Event (2007 System)

Occurs before the workbook is saved.

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

Syntax

'Declaration
Public Event BeforeSave As WorkbookEvents_BeforeSaveEventHandler
'Usage
Dim instance As Workbook 
Dim handler As WorkbookEvents_BeforeSaveEventHandler 

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

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

Workbook Class

Workbook Members

Microsoft.Office.Tools.Excel Namespace