共用方式為


Get started programming document-level customizations for Word

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

If you are just getting started creating document-level customizations for Microsoft Office Word by using Visual Studio, here is what you need to know.

Applies to: The information in this topic applies to document-level projects for Word. For more information, see Features available by Office application and project type.

Understand how document-level customizations for Word work

Each Word customization you create is based around a single document. To start using the customization, the end user opens the document or creates the document from a Word template. Events in the document, for example moving the cursor into specific areas or clicking buttons and menu items, can call event-handling methods in the assembly. When the document is closed, the features provided by the customization are no longer available in Word.

For more information, see Architecture of document-level customizations.

Create document-level projects for Word

To create a document-level customization for Word, use the Word Document or Word Template project template in the New Project dialog box. These templates include required assembly references and project files.

For more information about how to create a document-level project for Word, see How to: Create Office projects in Visual Studio. For more information about the project templates, see Office project templates overview.

Program Word documents by using host items host controls

Host items and host controls are classes that provide the programming model for document-level customizations.

Host items provide an entry point for your code, and they can also act as containers for host controls and Windows Forms controls. In document-level projects for Word, the host item is represented by the ThisDocument class.

Host controls are based on native Word objects, such as content controls, bookmarks, and XML nodes. Host controls provide similar functionality to the native Word objects, but they also have new events, designer support, and data-binding capability. They appear as first-class objects in your project code and in IntelliSense, which makes it easier to refer to specific objects directly in your code without having to navigate the Word object model.

For more information, see the following topics:

Customize the user interface of Word

Most Microsoft Office solutions modify the user interface (UI) of the Office application to provide some way for users to interact with the solution. There are many ways in which you can modify the UI of Word by using a document-level customization. For example, you can add controls to the ribbon, and you can display an actions pane. For more information, see Office UI customization.

You can also open the document that is associated with your project directly in Visual Studio. When the document is open in Visual Studio, you can modify the document by using the Word user interface. You can also use the document as a design surface, which enables you to drag controls onto it. For more information, see Office projects in the Visual Studio environment.

Bind controls to data

The content controls and the Bookmark control are in the list of controls that you can drag from the Data Sources window. Adding content controls and bookmarks in this way automatically binds them to the data source that you set up by using the window. Without writing any code, you can display data from databases, services, and business objects. For more information, see Bind data to controls in Office solutions.

Next steps

To learn how to create a document-level customization for Word, see Walkthrough: Create your first document-level customization for Word. This walkthrough introduces you to the Office development tools in Visual Studio and the programming model for Word document-level customizations.

For a list of topics that walk you through some of the common tasks in Word projects, see Common tasks in Office programming.

See also