共用方式為


WorkbookBase.ProtectDocument 方法

由 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 ProtectDocument
protected virtual void ProtectDocument()

備註

如果活頁簿是透過使用密碼所保護,且包含可能在執行階段變更的快取資料,則在 Excel 文件層級專案中覆寫這個方法。在這個方法的實作中,請使用 Protect 方法來保護活頁簿。

根據預設,當活頁簿儲存時,不會保存受密碼保護之活頁簿中的快取資料變更。若要儲存快取資料的變更,您必須覆寫專案中的下列方法:

  • UnprotectDocument.儲存活頁簿時,Visual Studio Tools for Office Runtime 會呼叫這個方法。為這個暫時取消保護活頁簿的方法加入程式碼。如此一來,就可以儲存快取資料的變更。

  • ProtectDocument.儲存活頁簿之後,Visual Studio Tools for Office Runtime 會呼叫這個方法。為這個重新套用保護功能至活頁簿的方法加入程式碼。

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

Visual Studio Tools for Office Runtime 會呼叫 ProtectDocument 方法,即使快取資料因為發生某個與密碼保護無關的錯誤而無法儲存時也會呼叫。例如,如果您實作 ICachedType 介面以自訂如何將快取資料儲存至文件,這時就會呼叫 ProtectDocument 方法,即使您的 ICachedType 實作擲回阻止儲存快取資料的例外狀況時也會呼叫。

範例

下列程式碼範例示範如何覆寫 ProtectDocument 方法,以重新套用因覆寫 UnprotectDocument 方法而移除的保護功能。若要使用這段程式碼,請在 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 命名空間

UnprotectDocument

其他資源

快取資料

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