This documentation is archived and is not being maintained.

Document Protection in Office Solutions

Note Required applications

The features in this topic are available only if you have the required applications installed.

For more information, see Features Available by Product Combination.

  • One of these development environments:

    VSTO 2005


    Visual Studio Team System

  • Microsoft Office 2003

You can use the protection features of Microsoft Office Word 2003 and Microsoft Office Excel 2003 in your Microsoft Visual Studio 2005 Tools for the Microsoft Office System 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 only turn protection on 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 with an empty password. If you want the document protection to have a password, you must add the password before you deploy your solution. Passwords enable authorized users to remove protection from the document; because there is no password, protection cannot be easily removed. For details about setting a password, see Help in the specific Office application.

See Also