本文為機器翻譯文章。如需檢視英文版,請選取 [原文] 核取方塊。您也可以將滑鼠指標移到文字上,即可在快顯視窗顯示英文原文。
譯文
原文

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 virtual void UnprotectDocument()

Override this method in a document-level project for Excel if your workbook is protected by using a password, and it contains cached data that might be changed at run time. In your implementation of this method, use the M:Microsoft.Office.Tools.Excel.WorkbookBase.Unprotect(System.Object) method to temporarily unprotect the workbook.

By default, changes to cached data in a password-protected workbook are not persisted when the workbook is saved. To save changes to the cached data, you must override the following methods in your project:

  • UnprotectDocument. When the workbook is saved, the Visual Studio Tools for Office Runtime calls this method. Add code to this method that temporarily unprotects the workbook. This enables changes to the cached data to be saved.

  • ProtectDocument. After the workbook is saved, the Visual Studio Tools for Office Runtime calls this method. Add code to this method that reapplies protection to the workbook.

For more information, see How to: Cache Data in a Password-Protected Document.

The following code example demonstrates how to override the M:Microsoft.Office.Tools.Excel.WorkbookBase.UnprotectDocument method to temporarily unprotect the workbook so that changes to the cached data can be saved. The example first saves the current P:Microsoft.Office.Tools.Excel.WorkbookBase.ProtectStructure and P:Microsoft.Office.Tools.Excel.WorkbookBase.ProtectWindows values, so that the same type of protection can be reapplied later in the M:Microsoft.Office.Tools.Excel.WorkbookBase.ProtectDocument method. To use this code, run it from the ThisWorkbook class in a document-level project for Excel. The code assumes that the password is stored in a field named securelyStoredPassword.

[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);
}
回到頁首
顯示: