Document Protection in Document-Level Solutions


You can use the protection features of Microsoft Office Word and Microsoft Office Excel in document-level projects. These features block unauthorized users from making changes to protected parts of a document.

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.

Using Excel, you can turn protection on and off while the workbook is open in the designer. Using Word, you can turn protection on only outside of the designer. At run time, you can enable or disable protection programmatically for both Word and Excel.

When document protection is enabled on a document that is open in the designer, all controls are removed from the Toolbox or are made unavailable, and you cannot drag anything from the Data Sources window to the document.

If a document is protected, the data cache cannot be accessed from outside of the document. You cannot use the ServerDocument class to retrieve or manipulate data that is cached in a protected document, or use other methods of the T:Microsoft.VisualStudio.Tools.Applications.Runtime.ServerDocument class.

If you add protection to a Word document or template while it is open in Visual Studio, you cannot start enforcing the protection in the designer. The document is in design mode while it is open in Visual Studio, and it must be in run mode before you can start enforcing protection.

However, if you create a project that uses an existing Word document that has protection enabled, the document is protected while open in the designer. You cannot edit the protected parts of the document, but you can still write code in the Code Editor to automate the document. You also cannot build the project if protection is enabled while the document is open in Visual Studio.

You can turn protection off while the document is open in the designer so that you can edit the document and build the project. You cannot turn off protection for the copy in the designer while you are debugging; the document that opens during debugging is a separate copy from the one open in the designer (the output copy is stored in the \bin directory for Visual Basic, and the \bin\debug directory for C#).

You can enable protection on the copy of the document that opens in the designer by closing the project in Visual Studio, opening the copy of the document that is in the project directory, and turning on protection.

Visual Studio starts enforcing protection for Word documents and templates during the build process, so that protection is enabled when the document opens for debugging. The document is protected with an empty password.

Protection is enabled during build so that if there is code in the document Startup event that might cause exceptions or change the behavior of the application, this code can be debugged correctly. If you enable protection after the document is opened, initialization code cannot be debugged or tested.

Visual Studio automatically enables protection, but provides no password by default. If you want the document protection to have a password, you must add it before you deploy your solution. Adding a password enables you to let authorized users remove protection from the document; without a password, protection cannot be easily removed. For details about setting a password, see Help in the specific Office application.

How to: Programmatically Protect Documents and Parts of Documents
Office Development Samples and Walkthroughs
Information Rights Management and Managed Code Extensions Overview
Password Protection on Office Documents
How to: Permit Code to Run Behind Documents with Restricted Permissions
Designing and Creating Office Solutions