Partilhar via


WorkbookBase.UnprotectDocument Method

Called by the Visual Studio Tools for Office runtime so that you can remove password protection from the workbook and enable cached data to be saved.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel.v4.0.Utilities (in Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)

Syntax

'Declaration
Protected Overridable Sub UnprotectDocument
protected virtual void UnprotectDocument()

Remarks

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 Unprotect 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.

Examples

The following code example demonstrates how to override the UnprotectDocument method to temporarily unprotect the workbook so that changes to the cached data can be saved. The example first saves the current ProtectStructure and ProtectWindows values, so that the same type of protection can be reapplied later in the 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 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 Security

See Also

Reference

WorkbookBase Class

Microsoft.Office.Tools.Excel Namespace

ProtectDocument

Other Resources

Caching Data

How to: Cache Data in a Password-Protected Document