Document protection in document-level solutions

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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.

ServerDocument and protected documents

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 ServerDocument class.

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 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.

Enforce Word document protection on build

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.

Setting the password

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.

See also