Document Protection in Document-Level Solutions

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

  • Microsoft Office 2003

  • 2007 Microsoft Office system

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

You can use the protection features of Microsoft Office Word and Microsoft Office Excel in your Visual Studio Tools for Office projects. These features block unauthorized users from making changes to protected parts of a document.

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.

ServerDocument and Protected Documents

If a document is protected, the cache cannot be accessed from outside of the document. You cannot use ServerDocument to retrieve or manipulate data that is cached in the document, or use other functions of ServerDocument.

Word Document Protection in the Designer

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 Visual Studio Tools for Office 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.

Enforcing Word Document Protection on Build

Visual Studio Tools for Office 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.

Setting the Password

Visual Studio Tools for Office 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.

See Also


How to: Protect Documents and Parts of Documents

Document Protector Sample

How to: Permit Code to Run Behind Documents with Restricted Permissions


Deploying Office Solutions (2003 System)

Information Rights Management and Managed Code Extensions Overview

Password Protection on Office Documents

Creating Office Solutions in Visual Studio