WorkbookBase.UnprotectDocument 方法

由 Visual Studio Tools for Office Runtime 调用,以便可以移除工作簿的密码保护,使缓存数据得以保存。

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

语法

声明
Protected Overridable Sub UnprotectDocument
protected virtual void UnprotectDocument()

备注

如果工作簿受密码保护,且包含可能在运行时发生更改的缓存数据,则应在 Excel 的文档级项目中重写此方法。实现此方法时,使用 Unprotect 方法暂时取消对工作簿的保护。

默认情况下,对于受密码保护的工作簿,保存工作簿时将不保持对其缓存数据所做的更改。若要保存对缓存数据所做的更改,必须重写项目中的下列方法:

  • UnprotectDocument.保存工作簿后,Visual Studio Tools for Office Runtime 将调用此方法。请向可暂时取消工作簿保护的此方法中添加代码。这使您可以保存对缓存数据所做的更改。

  • ProtectDocument.保存工作簿后,Visual Studio Tools for Office Runtime 将调用此方法。请向对工作簿重新应用保护的此方法中添加代码。

有关更多信息,请参见如何:在受密码保护的文档中缓存数据

示例

下面的代码示例演示如何重写 UnprotectDocument 方法,以便暂时取消对工作簿的保护,进而保存对缓存数据所做的更改。该示例首先会保存当前的 ProtectStructureProtectWindows 值,以便以后可以在 ProtectDocument 方法中重新应用相同类型的保护。若要使用此代码,请从 Excel 文档级项目的 ThisWorkbook 类中运行它。该代码假定密码存储在一个名为 securelyStoredPassword 的字段中。

<CachedAttribute()> _
Public CachedString As String = "This string is cached in the workbook."

Private protectStructureValue As Boolean
Private protectWindowsValue As Boolean

Protected Overrides Sub UnprotectDocument()
    protectStructureValue = Me.ProtectStructure
    protectWindowsValue = Me.ProtectWindows

    Me.Unprotect(securelyStoredPassword)
End Sub

Protected Overrides Sub ProtectDocument()
    Me.Protect(securelyStoredPassword, protectStructureValue, _
        protectWindowsValue)
End Sub
[CachedAttribute]
public string CachedString = "This string is cached in the workbook.";

private bool protectStructureValue;
private bool protectWindowsValue;

protected override void UnprotectDocument()
{
    protectStructureValue = this.ProtectStructure;
    protectWindowsValue = this.ProtectWindows;

    this.Unprotect(securelyStoredPassword);
}

protected override void ProtectDocument()
{
    this.Protect(securelyStoredPassword, protectStructureValue,
        protectWindowsValue);
}

.NET Framework 安全性

请参见

参考

WorkbookBase 类

Microsoft.Office.Tools.Excel 命名空间

ProtectDocument

其他资源

缓存数据

如何:在受密码保护的文档中缓存数据