Megosztás a következőn keresztül:


DocumentBase.ProtectDocument Method

Provides you with an opportunity to reapply password protection to the document after the cached data has been saved.

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

Syntax

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

Remarks

Override this method in a document-level project for Word if your document 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 Protect method to protect the document .

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

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

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

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

The Visual Studio Tools for Office runtime calls the 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 ICachedType interface to customize how cached data is stored in the document, the ProtectDocument method is called even if your ICachedType implementation throws an exception that prevents the cached data from being saved.

Examples

The following code example demonstrates how to override the ProtectDocument method to reapply protection that was removed by overriding the UnprotectDocument method. The code assumes that the password is stored in a field named securelyStoredPassword. To use this example, run it from the ThisDocument class in a document-level project.

<CachedAttribute()> _
Public CachedString As String = "This string is cached in the document." 

Private protectionTypeValue As Word.WdProtectionType

Protected Overrides Sub UnprotectDocument()
    If Me.ProtectionType <> Word.WdProtectionType.wdNoProtection Then
        protectionTypeValue = Me.ProtectionType
        Me.Unprotect(securelyStoredPassword)
    End If 
End Sub 

Protected Overrides Sub ProtectDocument()
    Me.Protect(protectionTypeValue, password:=securelyStoredPassword)
End Sub
[CachedAttribute]
public string CachedString = "This string is cached in the document.";

private Word.WdProtectionType protectionTypeValue;

protected override void UnprotectDocument()
{
    if (this.ProtectionType != Word.WdProtectionType.wdNoProtection)
    {
        protectionTypeValue = this.ProtectionType;
        this.Unprotect(ref securelyStoredPassword);
    }
}

protected override void ProtectDocument()
{
    this.Protect(protectionTypeValue, ref missing,
        ref securelyStoredPassword, ref missing, ref missing);
}

.NET Framework Security

See Also

Reference

DocumentBase Class

Microsoft.Office.Tools.Word Namespace

UnprotectDocument

Other Resources

Caching Data

How to: Cache Data in a Password-Protected Document