Share via


Workbook.Saved Property (2007 System)

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

Syntax

'Declaration
<BrowsableAttribute(False)> _
Public Property Saved As Boolean
'Usage
Dim instance As Workbook 
Dim value As Boolean 

value = instance.Saved

instance.Saved = value
[BrowsableAttribute(false)]
public bool Saved { get; set; }
[BrowsableAttribute(false)]
public:
property bool Saved {
    bool get ();
    void set (bool value);
}
public function get Saved () : boolean 
public function set Saved (value : boolean)

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

Workbook Class

Workbook Members

Microsoft.Office.Tools.Excel Namespace