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 會呼叫這個方法。 為這個重新套用保護功能至活頁簿的方法加入程式碼。

如需詳細資訊,請參閱HOW TO:快取受密碼保護文件中的資料

範例

下列程式碼範例示範如何覆寫 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

其他資源

快取資料

HOW TO:快取受密碼保護文件中的資料