Export (0) Print
Expand All
Expand Minimize

Workbook.Saved Property

Gets or sets a value that indicates whether no changes have been made to the workbook since it was last saved.

Namespace: Microsoft.Office.Tools.Excel
Assembly: Microsoft.Office.Tools.Excel (in microsoft.office.tools.excel.dll)

public virtual bool Saved { get; set; }

Property Value

true if no changes have been made to the workbook since it was last saved; otherwise, false.

If a workbook has never been saved, its Path property returns an empty string ("").

You can set this property to true if you want to close a modified workbook without either saving it or being prompted to save it.

The following code example demonstrates a handler for the BeforeClose event that prompts the user to either save changes, not save changes, or cancel the close operation if changes have been made to the workbook since it was last saved. If the user does not save changes, then the Saved property of the workbook is set to true so that Microsoft Office Excel does not prompt the user to save the workbook when the close operation continues. If the user cancels the close operation, then the Cancel parameter of the WorkbookEvents_BeforeCloseEventHandler event handler is set to true so that Microsoft Office Excel does not close the workbook.

private void WorkbookBeforeClose()
{
    this.BeforeClose +=
        new Excel.WorkbookEvents_BeforeCloseEventHandler(
        ThisWorkbook_BeforeClose);
}

void ThisWorkbook_BeforeClose(ref bool Cancel)
{
    if (!this.Saved)
    {
        DialogResult result = MessageBox.Show("Do you want to save the " +
            "changes you made to " + this.Name + "?", "Example", 
            MessageBoxButtons.YesNoCancel);

        switch (result)
        {
            case DialogResult.Yes:
                this.Save();
                break;

            case DialogResult.Cancel:
                Cancel = true;
                break;

            // The following code ensures that the default Save File 
            // dialog is not displayed.
            case DialogResult.No:
                this.Saved = true;
                break;
        }
    }
}

Show:
© 2014 Microsoft