Share via


HOW TO:快取受密碼保護文件中的資料

如果您將資料加入至設有密碼保護之文件或活頁簿中的資料快取,則不會自動儲存快取資料的變更。 您可以藉由覆寫專案中的兩個方法儲存快取資料的變更。

**適用於:**本主題中的資訊適用於下列應用程式的文件層級專案:Excel 2007 和 Excel 2010、Word 2007 和 Word 2010。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

在 Word 文件中進行快取

若要快取受密碼保護之 Word 文件中的資料

  1. 在 ThisDocument 類別中,標記要加以快取的公用 (Public) 欄位或屬性。 如需詳細資訊,請參閱 快取資料

  2. 覆寫 ThisDocument 類別中的 DocumentBase.UnprotectDocument 方法,並從該文件移除保護。

    儲存文件時,Visual Studio Tools for Office Runtime 會呼叫此方法,讓您有機會取消保護文件, 如此一來,就可以儲存快取資料的變更。

  3. 覆寫 ThisDocument 類別中的 DocumentBase.ProtectDocument 方法,並為文件重新套用保護。

    儲存文件之後,Visual Studio Tools for Office Runtime 會呼叫此方法,讓您有機會將保護重新套用至文件。

範例

下列程式碼範例示範如何快取受密碼保護之 Word 文件中的資料。 在以 DocumentBase.UnprotectDocument 方法移除保護之前,這段程式碼會儲存目前的 ProtectionType 值,以便日後可透過 DocumentBase.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 活頁簿中的資料

  1. 在 ThisWorkbook 類別或其中一個 Sheetn 類別中,標記要加以快取的公用欄位或屬性。 如需詳細資訊,請參閱 快取資料

  2. 覆寫 ThisWorkbook 類別中的 WorkbookBase.UnprotectDocument 方法,並從活頁簿移除保護。

    儲存活頁簿時,Visual Studio Tools for Office Runtime 會呼叫此方法,讓您有機會取消保護活頁簿, 如此一來,就可以儲存快取資料的變更。

  3. 覆寫 ThisWorkbook 類別中的 WorkbookBase.ProtectDocument 方法,並為文件重新套用保護。

    儲存活頁簿之後,Visual Studio Tools for Office Runtime 會呼叫此方法,讓您有機會將保護重新套用至活頁簿。

範例

下列程式碼範例示範如何快取受密碼保護之 Excel 活頁簿中的資料。 在以 WorkbookBase.UnprotectDocument 方法移除保護之前,這段程式碼會儲存目前的 ProtectStructureProtectWindows 值,以便日後可透過 WorkbookBase.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 的欄位中。

請參閱

工作

HOW TO:快取資料供離線使用或於伺服器上使用

HOW TO:以程式設計方式快取 Office 文件的資料來源

概念

快取資料