Share via


WorkbookBase.Saved Property

Definition

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

public:
 property bool Saved { bool get(); void set(bool value); };
public bool Saved { get; set; }
member this.Saved : bool with get, set
Public Property Saved As Boolean

Property Value

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

Examples

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.

This example is for a document-level customization.

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;
        }
    }
}
Sub ThisWorkbook_BeforeClose(ByRef Cancel As Boolean) _
    Handles Me.BeforeClose

    If Not Me.Saved Then
        Dim result As DialogResult = _
            MessageBox.Show("Do you want to save the " & _
            "changes you made to " & Me.Name & "?", _
            "Example", MessageBoxButtons.YesNoCancel)

        Select Case result
            Case DialogResult.Yes
                Me.Save()
            Case DialogResult.Cancel
                Cancel = True
                ' The following code ensures that the default Save File 
                ' dialog is not displayed.
            Case DialogResult.No
                Me.Saved = True
        End Select
    End If
End Sub

Remarks

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.

Applies to