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


How to: Cache Data in a Password-Protected Document

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

Microsoft Office version

  • 2007 Microsoft Office system

For more information, see Features Available by Application and Project Type.

In document-level projects for Word 2007 and Excel 2007, if you add data to the data cache in a document or workbook that is protected with a password, changes to the cached data are not saved.

Starting in Visual Studio 2008 Service Pack 1 (SP1), you can save changes to the cached data by overriding two methods in your project.

Caching in Word Documents

To cache data in a Word document that is protected with a password

  1. In the ThisDocument class, mark a public field or property to be cached. For more information, see Caching Data.

  2. Override the Document.UnprotectDocument method in the ThisDocument class and remove protection from the document.

    When the document is saved, the Visual Studio Tools for Office runtime calls this method to give you an opportunity to unprotect the document. This enables changes to the cached data to be saved.

  3. Override the Document.ProtectDocument method in the ThisDocument class and reapply protection to the document.

    After the document is saved, the Visual Studio Tools for Office runtime calls this method to give you an opportunity to reapply protection to the document.

Example

The following code example demonstrates how to cache data in a Word document that is protected with a password. Before the code removes the protection in the Document.UnprotectDocument method, it saves the current ProtectionType value, so that the same type of protection can be reapplied in the Document.ProtectDocument method.

<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);
}

Compiling the Code

Add this code to the ThisDocument class in your project. This code assumes that the password is stored in a field named securelyStoredPassword.

Caching in Excel Workbooks

In Excel projects, this procedure is necessary only when you protect the entire workbook with a password by using the Workbook.Protect method. This procedure is not necessary if you protect only a specific worksheet with a password by using the Worksheet.Protect method.

To cache data in an Excel workbook that is protected with a password

  1. In the ThisWorkbook class or one of the Sheetn classes, mark a public field or property to be cached. For more information, see Caching Data.

  2. Override the Workbook.UnprotectDocument method in the ThisWorkbook class and remove protection from the workbook.

    When the workbook is saved, the Visual Studio Tools for Office runtime calls this method to give you an opportunity to unprotect the workbook. This enables changes to the cached data to be saved. 

  3. Override the Workbook.ProtectDocument method in the ThisWorkbook class and reapply protection to the document.

    After the workbook is saved, the Visual Studio Tools for Office runtime calls this method to give you an opportunity to reapply protection to the workbook.

Example

The following code example demonstrates how to cache data in an Excel workbook that is protected with a password. Before the code removes the protection in the Workbook.UnprotectDocument method, it saves the current ProtectStructure and ProtectWindows values, so that the same type of protection can be reapplied in the Workbook.ProtectDocument method.

<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);
}

Compiling the Code

Add this code to the ThisWorkbook class in your project. This code assumes that the password is stored in a field named securelyStoredPassword.

See Also

Tasks

How to: Cache Data for Use Offline or on a Server

How to: Programmatically Cache a Data Source in an Office Document

Concepts

Caching Data

Change History

Date

History

Reason

July 2008

New topic.

SP1 feature change.