How to: Cache Data in a Password-Protected Document
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 automatically. You can save changes to the cached data by overriding two methods in your project.
Applies to: The information in this topic applies to document-level projects for Excel and Word. For more information, see Features Available by Office Application and Project Type.
To cache data in a Word document that is protected with a password
In the
ThisDocumentclass, mark a public field or property to be cached. For more information, see Caching Data.Override the DocumentBase.UnprotectDocument method in the
ThisDocumentclass 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.
Override the DocumentBase.ProtectDocument method in the
ThisDocumentclass 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 DocumentBase.UnprotectDocument method, it saves the current ProtectionType value, so that the same type of protection can be reapplied in the DocumentBase.ProtectDocument method.
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.
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
In the
ThisWorkbookclass or one of theSheetn classes, mark a public field or property to be cached. For more information, see Caching Data.Override the WorkbookBase.UnprotectDocument method in the
ThisWorkbookclass 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.
Override the WorkbookBase.ProtectDocument method in the
ThisWorkbookclass 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 WorkbookBase.UnprotectDocument method, it saves the current ProtectStructure and ProtectWindows values, so that the same type of protection can be reapplied in the WorkbookBase.ProtectDocument method.
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.
Caching Data
How to: Cache Data for Use Offline or on a Server
How to: Programmatically Cache a Data Source in an Office Document