Word 对象模型概述Word object model overview

在 Visual Studio 中开发 Word 解决方案时,会与 Word 对象模型进行交互。When you develop Word solutions in Visual Studio, you interact with the Word object model. 此对象模型包含 Word 的主互操作程序集中所提供的类和接口,并在 Microsoft.Office.Interop.Word 命名空间中进行定义。This object model consists of classes and interfaces that are provided in the primary interop assembly for Word, and are defined in the Microsoft.Office.Interop.Word namespace.

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

本主题概要介绍 Word 对象模型。This topic provides a brief overview of the Word object model. 有关可了解整个 Word 对象模型的详细信息的资源,请参阅使用 word 对象模型文档For resources where you can learn more about the entire Word object model, see Use the Word object model documentation.

有关使用 Word 对象模型执行特定任务的信息,请参阅下列主题:For information about using the Word object model to perform specific tasks, see the following topics:

了解 Word 对象模型Understand the Word object model

Word 提供了数百个可与之交互的对象。Word provides hundreds of objects with which you can interact. 这些对象采用严格遵循用户界面的层次结构进行组织。These objects are organized in a hierarchy that closely follows the user interface. Application 对象位于层次结构的顶部。At the top of the hierarchy is the Application object. 此对象表示 Word 的当前实例。This object represents the current instance of Word. Application 对象包含 DocumentSelectionBookmarkRange 对象。The Application object contains the Document, Selection, Bookmark, and Range objects. 上述每个对象均具有很多方法和属性,可用于操作对象和与之进行交互。Each of these objects has many methods and properties that you can access to manipulate and interact with the object.

下图显示了 Word 对象模型层次结构中这些对象的一个视图。The following illustration shows one view of these objects in the hierarchy of the Word object model.

Word 对象模型图Word Object Model graphic

初看起来,对象似乎重叠在一起。At first glance, objects appear to overlap. 例如, DocumentSelection 对象都是 Application 对象的成员,但 Document 对象也是 Selection 对象的成员。For example, the Document and Selection objects are both members of the Application object, but the Document object is also a member of the Selection object. DocumentSelection 对象都包含 BookmarkRange 对象。Both the Document and Selection objects contain Bookmark and Range objects. 因为有多种方法可以访问相同类型的对象,所以存在重叠。The overlap exists because there are multiple ways you can access the same type of object. 例如,你将格式设置应用于 Range 对象;但你可能想要访问当前选定内容、某一特定段落,某一节或整个文档的范围。For example, you apply formatting to a Range object; but you may want to access the range of the current selection, of a particular paragraph, of a section, or of the entire document.

下列各节简要介绍了顶级对象以及它们彼此交互的方式。The following sections briefly describe the top-level objects and how they interact with each other. 这些对象包括下列五种:These objects include the following five:

  • 应用程序对象Application object

  • 文档对象Document object

  • Selection 对象Selection object

  • Range 对象Range object

  • Bookmark 对象Bookmark object

    除 Word 对象模型以外,Visual Studio 中的 Office 项目还提供可扩展 Word 对象模型中的一些对象的 主机项主机控件In addition to the Word object model, Office projects in Visual Studio provide host items and host controls that extend some objects in the Word object model. 主机项和主机控件的行为类似于它们扩展的 Word 对象,但它们还具有其他功能(如数据绑定功能)和额外事件。Host items and host controls behave like the Word objects they extend, but they also have additional functionality such as data-binding capabilities and extra events. 有关详细信息,请参阅使用扩展对象实现 Word 自动化主机项和主机控件概述For more information, see Automate Word by using extended objects and Host items and host controls overview.

应用程序对象Application object

Application 对象表示 Word 应用程序,并且是所有其他对象的父级。The Application object represents the Word application, and is the parent of all of the other objects. 其成员通常作为一个整体应用于 Word。Its members usually apply to Word as a whole. 你可以使用其属性和方法来控制 Word 环境。You can use its properties and methods to control the Word environment.

在 VSTO 外接程序项目中,可以通过使用 Application 类的 Application 字段来访问 ThisAddIn 对象。In VSTO Add-in projects, you can access the Application object by using the Application field of the ThisAddIn class. 有关详细信息,请参阅PROGRAM VSTO 外接程序For more information, see Program VSTO Add-ins.

在文档级项目中,可以通过使用 Application 类的 Application 属性来访问 ThisDocument 对象。In document-level projects, you can access the Application object by using the Application property of the ThisDocument class.

文档对象Document object

Document 对象是 Word 编程的中心。The Document object is central to programming Word. 它表示一个文档及其所有内容。It represents a document and all of its contents. 当你打开文档或创建新文档时,将创建新的 Document 对象,并将其添加到 Documents 对象的 Application 集合。When you open a document or create a new document, you create a new Document object, which is added to the Documents collection of the Application object. 具有焦点的文档被称为活动文档。The document that has the focus is called the active document. 它由 ActiveDocument 对象的 Application 属性表示。It is represented by the ActiveDocument property of the Application object.

Visual Studio 中的 Office 开发工具通过提供 Document 类型来扩展 Document 对象。The Office development tools in Visual Studio extend the Document object by providing the Document type. 此类型是一个 主机项 ,使你可以访问 Document 对象的所有功能,并增添了其他事件以及添加托管控件的能力。This type is a host item that gives you access to all features of a Document object, and adds additional events and the ability to add managed controls.

在创建文档级项目时,可以通过使用项目中生成的 Document 类访问 ThisDocument 成员。When you create a document-level project, you can access Document members by using the generated ThisDocument class in your project. 通过使用 Document 类中代码的 Methis 关键字、或通过使用 ThisDocument 类外部的代码的 Globals.ThisDocument ,即可访问 ThisDocument 对象。You can access members of the Document host item by using the Me or this keywords from code in the ThisDocument class, or by using Globals.ThisDocument from code outside the ThisDocument class. 有关详细信息,请参阅程序文档级自定义项For more information, see Program document-level customizations. 例如,若要在文档中选择第一个段落,请使用下列代码。For example, to select the first paragraph in the document, use the following code.


在 VSTO 外接程序项目中,可以在运行时生成 Document 主机项。In VSTO Add-in projects, you can generate Document host items at run time. 可以使用生成的主机项将控件添加到关联文档。You can use the generated host item to add controls to the associated document. 有关详细信息,请参阅在运行时在 VSTO 外接程序中扩展 Word 文档和 Excel 工作簿For more information, see Extend Word documents and Excel workbooks in VSTO Add-ins at run time.

Selection 对象Selection object

Selection 对象表示当前所选的区域。The Selection object represents the area that is currently selected. 在 Word 用户界面中执行操作(如文本加粗)时,可以选择或突出显示文本,然后应用格式设置。When you perform an operation in the Word user interface, such as bolding text, you select, or highlight the text and then apply the formatting. 文档中始终存在 Selection 对象。The Selection object is always present in a document. 如果未选中任何内容,则它表示插入点。If nothing is selected, then it represents the insertion point. 此外,选定内容可包含多个不相邻的文本块。In addition, a selection can encompass multiple blocks of text that are not contiguous.

Range 对象Range object

Range 对象表示文档中的相邻区域,并由起始字符位置和结束字符位置进行定义。The Range object represents a contiguous area in a document, and is defined by a starting character position and an ending character position. 并不仅限于单个 Range 对象。You are not limited to a single Range object. 你可以在同一文档中定义多个 Range 对象。You can define multiple Range objects in the same document. Range 对象具有以下特性:A Range object has the following characteristics:

  • 它可以只包含单独的插入点,也可包含一个文本范围或整个文档。It can consist of the insertion point alone, a range of text, or the entire document.

  • 它包括非打印字符,如空格、制表符和段落标记。It includes non-printing characters such as spaces, tab characters, and paragraph marks.

  • 它可以是当前选定内容所表示的区域,也可以表示不同于此内容的区域。It can be the area represented by the current selection, or it can represent an area different from the current selection.

  • 它在文档中不可见,这与选定内容不同,后者总是可见。It is not visible in a document, unlike a selection, which is always visible.

  • 它不随文档一起保存,且仅在代码运行时才存在。It is not saved with a document and exists only while the code is running.

    当在某个范围的末尾插入文本时,Word 会自动扩展该范围以包括插入的文本。When you insert text at the end of a range, Word automatically expands the range to include the inserted text.

内容控件对象Content control objects

ContentControl 提供一种用于控制 Word 文档内文本和其他类型的内容的输入和呈现的方法。A ContentControl provides a way for you to control the input and presentation of text and other types of content in Word documents. ContentControl 可以显示多种不同类型的 UI,它们进行了优化以在 Word 文档中使用,如多信息文本控件、日期选取器或组合框。A ContentControl can display several different types of UI that are optimized for use in Word documents, such as a rich text control, a date picker, or a combo box. 你还可以使用 ContentControl 来防止用户编辑文档或模板的某些节。You can also use a ContentControl to prevent users from editing sections of the document or template.

Visual Studio 会将 ContentControl 对象扩展到几个不同的主机控件。Visual Studio extends the ContentControl object into several different host controls. 虽然 ContentControl 对象能显示可用于内容控件的所有不同类型的 UI,Visual Studio 还是为每个内容控件提供了一个不同的类型。Whereas the ContentControl object can display any of the different types of UI that are available for content controls, Visual Studio provides a different type for each content control. 例如,你可以使用 RichTextContentControl 来创建多信息文本控件,或者可以使用 DatePickerContentControl 来创建日期选取器。For example, you can use a RichTextContentControl to create a rich text control, or you can use a DatePickerContentControl to create a date picker. 这些主机控件的行为与本机 ContentControl的类似,但它们还具有其他事件和数据绑定功能。These host controls behave like the native ContentControl, but they have additional events and data-binding capabilities. 有关详细信息,请参阅内容控件For more information, see Content controls.

Bookmark 对象Bookmark object

Bookmark 对象表示文档中的相邻区域,同时具有起始位置和结束位置。The Bookmark object represents a contiguous area in a document, with both a starting position and an ending position. 你可以使用书签标记文档中的某个位置,也可将其作为文档中文本的容器。You can use bookmarks to mark a location in a document, or as a container for text in a document. Bookmark 对象可以包含插入点,也可以与整个文档一样大。A Bookmark object can consist of the insertion point, or be as large as the entire document. Bookmark 具有下列特征,以将其与 Range 对象区别开来:A Bookmark has the following characteristics that set it apart from the Range object:

  • 你可以在设计时命名书签。You can name the bookmark at design time.

  • Bookmark 对象随文档一起保存,因此在代码停止运行或文档关闭时不会被删除。Bookmark objects are saved with the document, and thus are not deleted when the code stops running or your document is closed.

  • 通过将 ShowBookmarks 属性来访问 View 属性设置为 falsetrueBookmarks can be hidden or made visible by setting the ShowBookmarks property of the View object to false or true.

    Visual Studio 通过提供 Bookmark 主机控件来扩展 Bookmark 对象。Visual Studio extends the Bookmark object by providing the Bookmark host control. Bookmark 主机控件的行为与本机 Bookmark的类似,但它们还具有其他事件和数据绑定功能。The Bookmark host control behaves like a native Bookmark, but has additional events and data-binding capabilities. 你可以将数据绑定到文档上的书签控件,操作方式与将数据绑定到 Windows 窗体上文本框控件的方式相同。You can bind data to a bookmark control on a document in the same way that you bind data to a text box control on a Windows Form. 有关详细信息,请参阅书签控件For more information, see Bookmark control.

使用 Word 对象模型文档Use the Word object model documentation

有关 Word 对象模型的完整信息,可以参考 Word 主互操作程序集 (PIA) 引用和 Visual Basic for Applications (VBA) 对象模型引用。For complete information about the Word object model, you can refer to the Word primary interop assembly (PIA) reference and the Visual Basic for Applications (VBA) object model reference.

主互操作程序集引用Primary interop assembly reference

Word PIA 参考文档介绍了 Word 的主互操作程序集中的类型。The Word PIA reference documentation describes the types in the primary interop assembly for Word. 此文档可从以下位置获取: Word 2010 主互操作程序集引用This documentation is available from the following location: Word 2010 primary interop assembly reference.

有关 Word PIA 设计的详细信息(例如 PIA 中类和接口之间的差异以及如何实现 PIA 中的事件),请参阅Office 主互操作程序集中的类和接口的概述For more information about the design of the Word PIA, such as the differences between classes and interfaces in the PIA and how events in the PIA are implemented, see Overview of classes and interfaces in the Office primary interop assemblies.

VBA 对象模型引用VBA object model reference

VBA 对象模型引用在将 Word 对象模型公开到 VBA 代码时对该对象进行了记录。The VBA object model reference documents the Word object model as it is exposed to VBA code. 有关详细信息,请参阅Word 2010 对象模型引用For more information, see Word 2010 object model reference.

VBA 对象模型引用中的所有对象和成员都与 Word PIA 中的类型和成员相对应。All of the objects and members in the VBA object model reference correspond to types and members in the Word PIA. 例如,VBA 对象模型引用中的文档对象对应于 Word PIA 中的 Document 对象。For example, the Document object in the VBA object model reference corresponds to the Document object in the Word PIA. 虽然 VBA 对象模型引用提供了大多数属性、方法和事件的代码示例,但如果要在用 Visual Studio 创建的 Word 项目中使用本引用中的 VBA 代码,必须将其转换为 Visual Basic 或 Visual C# 代码。Although the VBA object model reference provides code examples for most properties, methods, and events, you must translate the VBA code in this reference to Visual Basic or Visual C# if you want to use them in a Word project that you create by using Visual Studio.

请参阅See also