更新: 2008 年 7 月
| 适用于 | 
|---|
| 本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。 项目类型 
 Microsoft Office 版本 
 有关更多信息,请参见按应用程序和项目类型提供的功能。 | 
在 Word 2007 和 Excel 2007 的文档级项目中,如果在受密码保护的文档或工作簿中向数据缓存添加数据,将不保存对缓存数据所做的更改。
从 Visual Studio 2008 Service Pack 1 (SP1) 开始,您可以通过在项目中重写两个方法来保存对缓存数据所做的更改。
在 Word 文档中执行缓存
在受密码保护的 Word 文档中缓存数据
- 在 ThisDocument 类中,标记要缓存的公共字段或属性。有关更多信息,请参见缓存数据。 
- 在 ThisDocument 类中重写 Document.UnprotectDocument 方法,取消对文档的保护。 - 保存文档时,Visual Studio Tools for Office 运行时会调用此方法,以便为您提供取消文档保护的机会。这使您可以保存对缓存数据所做的更改。 
- 在 ThisDocument 类中重写 Document.ProtectDocument 方法,对文档重新应用保护。 - 保存文档后,Visual Studio Tools for Office 运行时会调用此方法,以便为您提供对文档重新应用保护的机会。 
示例
下面的代码示例演示如何在受密码保护的 Word 文档中缓存数据。代码在 Document.UnprotectDocument 方法中取消保护之前,将保存当前的 ProtectionType 值,以便可以在 Document.ProtectDocument 方法中重新应用相同类型的保护。
<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);
}
编译代码
将此代码添加到项目的 ThisDocument 类中。此代码假定密码存储在一个名为 securelyStoredPassword 的字段中。
在 Excel 工作簿中执行缓存
在 Excel 项目中,仅当通过 Workbook.Protect 方法使用密码保护整个工作簿后,才需要执行此过程。如果通过 Worksheet.Protect 方法使用密码来保护某个特定的工作簿,则不需要执行此过程。
在受密码保护的 Excel 工作簿中缓存数据
- 在 ThisWorkbook 类或某个 Sheetn 类中,标记要缓存的公共字段或属性。有关更多信息,请参见缓存数据。 
- 在 ThisWorkbook 类中重写 Workbook.UnprotectDocument 方法,取消对工作簿的保护。 - 保存工作簿时,Visual Studio Tools for Office 运行时会调用此方法,以便为您提供取消工作簿保护的机会。这使您可以保存对缓存数据所做的更改。 
- 在 ThisWorkbook 类中重写 Workbook.ProtectDocument 方法,对工作簿重新应用保护。 - 保存工作簿后,Visual Studio Tools for Office 运行时会调用此方法,以便为您提供对工作簿重新应用保护的机会。 
示例
下面的代码示例演示如何在受密码保护的 Excel 工作簿中缓存数据。代码在 Workbook.UnprotectDocument 方法中取消保护之前,将保存当前的 ProtectStructure 和 ProtectWindows 值,以便可以在 Workbook.ProtectDocument 方法中重新应用相同类型的保护。
<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);
}
编译代码
将此代码添加到项目的 ThisWorkbook 类中。此代码假定密码存储在一个名为 securelyStoredPassword 的字段中。
请参见
任务
概念
修订记录
| 日期 | 修订历史记录 | 原因 | 
|---|---|---|
| 2008 年 7 月 | 新增主题。 | SP1 功能更改。 |