共用方式為


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 the following applications: Excel 2013 and Excel 2010; Word 2013 and Word 2010. 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.

Enforcing 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

Tasks

How to: Programmatically Protect Documents and Parts of Documents

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

Concepts

Information Rights Management and Managed Code Extensions Overview

Password Protection on Office Documents

Other Resources

Office Development Samples and Walkthroughs

Designing and Creating Office Solutions