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

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

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.Protect(System.Object,System.Object,System.Object) method to protect 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 vsto_runtime calls the M:Microsoft.Office.Tools.Excel.WorkbookBase.ProtectDocument method even if the cached data could not be saved because of some error that is unrelated to the password protection. For example, if you implement the T:Microsoft.VisualStudio.Tools.Applications.Runtime.ICachedType interface to customize how cached data is stored in the document, the M:Microsoft.Office.Tools.Excel.WorkbookBase.ProtectDocument method is called even if your T:Microsoft.VisualStudio.Tools.Applications.Runtime.ICachedType implementation throws an exception that prevents the cached data from being saved.

The following code example demonstrates how to override the M:Microsoft.Office.Tools.Excel.WorkbookBase.ProtectDocument method to reapply protection that was removed by overriding the M:Microsoft.Office.Tools.Excel.WorkbookBase.UnprotectDocument 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);
}
回到頁首
顯示: