WorkbookBase.Saved 属性

获取或设置一个值,该值指示工作簿自上次保存以来是否进行了更改。

命名空间:  Microsoft.Office.Tools.Excel
程序集:  Microsoft.Office.Tools.Excel.v4.0.Utilities(在 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll 中)

语法

声明
Public Property Saved As Boolean
    Get
    Set
public bool Saved { get; set; }

属性值

类型:System.Boolean
如果工作簿自上次保存以来进行了更改,则为 true;否则为 false。

备注

如果工作簿从未保存过,则其 Path 属性返回空字符串 ("")。

如果希望关闭修改过的工作簿而不进行保存或不被提示进行保存,则可将此属性设置为 true。

示例

下面的代码示例演示 BeforeClose 事件的处理程序,如果工作簿自上次保存以来进行了更改,则该处理程序会提示用户是否保存更改,或取消关闭操作。 如果用户不保存更改,则工作簿的 Saved 属性设置为 true,以便在关闭操作继续时 Microsoft Office Excel 不会提示用户保存工作簿。 如果用户取消关闭操作,则 WorkbookEvents_BeforeCloseEventHandler 事件处理程序的 Cancel 参数设置为 true,以便 Microsoft Office Excel 不关闭工作簿。

此示例针对的是文档级自定义项。

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 安全性

请参见

参考

WorkbookBase 类

Microsoft.Office.Tools.Excel 命名空间