Word 文档级自定义项编程入门Get started programming document-level customizations for Word

如果你刚开始使用 Visual Studio 创建 Microsoft Office Word 文档级自定义项,下面是您需要了解。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.

适用于: 本主题中的信息适用于文档-Word 级别项目。Applies to: The information in this topic applies to document-level projects for Word. 有关详细信息,请参阅按 Office 应用程序和项目类型提供的功能For more information, see Features available by Office application and project type.

了解 Word 工作的文档级自定义项Understand how document-level customizations for Word work

您创建每个 Word 自定义基于单个文档。Each Word customization you create is based around a single document. 若要开始使用自定义项,最终用户打开文档或从 Word 模板创建文档。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. 关闭文档时,提供自定义项的功能将不再在 Word 中可用。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.

为 Word 创建文档级项目Create document-level projects for Word

若要创建 Word 文档级自定义,请使用中的 Word 文档或 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.

有关如何创建 Word 文档级项目的详细信息,请参阅如何:在 Visual Studio 中创建 Office 项目For more information about how to create a document-level project for Word, see How to: Create Office projects in Visual Studio. 有关项目模板的详细信息,请参阅Office 项目模板概述For more information about the project templates, see Office project templates overview.

程序通过使用主机项宿主控件的 Word 文档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.

宿主项提供的入口点为你的代码,并且它们还可以充当宿主控件和 Windows 窗体控件的容器。Host items provide an entry point for your code, and they can also act as containers for host controls and Windows Forms controls. 在 word 文档级项目中,主机项表示由ThisDocument类。In document-level projects for Word, the host item is represented by the ThisDocument class.

主机控件基于本机 Word 对象,如内容控件、 书签和 XML 节点。Host controls are based on native Word objects, such as content controls, bookmarks, and XML nodes. 宿主控件提供本机 Word 对象,与类似的功能,但它们还具有新的事件、 设计器支持和数据绑定功能。Host controls provide similar functionality to the native Word objects, but they also have new events, designer support, and data-binding capability. 它们显示为在你的项目代码和 IntelliSense,因此可以轻松地引用直接在代码中的特定对象,而无需导航 Word 对象模型中的第一类对象。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:

自定义 Word 的用户界面Customize the user interface of Word

大多数 Microsoft Office 解决方案的 Office 应用程序提供用户交互的解决方案某种方式修改用户界面 (UI)。Most Microsoft Office solutions modify the user interface (UI) of the Office application to provide some way for users to interact with the solution. 有许多方法通过使用文档级自定义可以在其中修改 Word 的 UI。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. 有关详细信息,请参阅Office UI 自定义For more information, see Office UI customization.

此外可以打开与你直接在 Visual Studio 中的项目相关联的文档。You can also open the document that is associated with your project directly in Visual Studio. 在 Visual Studio 中打开文档时,可以通过使用 Word 用户界面来修改文档。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. 有关详细信息,请参阅Visual Studio 环境中的 Office 项目For more information, see Office projects in the Visual Studio environment.

将控件绑定到数据Bind controls to data

内容控件和Bookmark控制是在列表中的控件,您可以从拖动数据源窗口。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. 有关详细信息,请参阅将数据绑定到 Office 解决方案中的控件For more information, see Bind data to controls in Office solutions.

后续步骤Next steps

若要了解如何创建 Word 文档级自定义,请参阅演练:创建在第一个文档级自定义 wordTo learn how to create a document-level customization for Word, see Walkthrough: Create your first document-level customization for Word. 本演练向您介绍 Visual Studio 和 Word 文档级自定义项的编程模型中的 Office 开发工具。This walkthrough introduces you to the Office development tools in Visual Studio and the programming model for Word document-level customizations.

有关指导你完成某些在 Word 项目中的常见任务的主题的列表,请参阅Office 编程中的常见任务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