Share via


WorkbookBase.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.v4.0.Utilities (in Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)

Syntax

'Declaration
Public Property Saved As Boolean
    Get
    Set
public bool Saved { get; set; }

Property Value

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

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.

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.

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
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;
        }
    }
}

.NET Framework Security

See Also

Reference

WorkbookBase Class

Microsoft.Office.Tools.Excel Namespace