Document host item

The Document host item is a type that extends the Document type from the primary interop assembly for Word. The Document host item provides all of the same properties, methods, and events as a Document object, but it also exposes additional events and acts as a container for host controls and Windows Forms controls.

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

In document-level projects, there is a default Document host item that represents the document in your project. In VSTO Add-in projects, you can generate Document host items at run time.

Understand the document host item in document-level projects

To access the document in your project, use the ThisDocument class. When you create a document-level project, Visual Studio generates the ThisDocument class to serve as the communication link between Word and your customization code. The ThisDocument class gives you access to members of the Document host item to perform basic tasks in your customization, such as running code when the document is opened or closed. You can also use the class to add controls to the document. By combining different sets of controls and writing code, you can bind the controls to data, collect information from the user, and respond to user actions. For more information, see Program document-level customizations.

The ThisDocument class provides a location in which you can start writing code in your project. Because the class provides all of the same properties, methods, and events as the Document object in the primary interop assembly for Word, you can also use ThisDocument to access the object model of Word. For more information, see Word object model overview.

Limitations of the document host item in document-level projects

A document-level project can contain only one Document host item (that is, the ThisDocument class). You cannot add new Document host items to your project at design time, and you cannot create new Document host items at runtime from a document-level customization.

If you create a new Word document at runtime, it will be of the type Document. Because it is not a host item, it cannot contain any host controls or Windows Forms controls. For more information about creating documents at run time, see How to: Programmatically create new documents.

Understand document host items in application-level projects

In VSTO Add-in projects, you can generate a Document host item at run time for any document that is open in Word. You can use the Document host item to add controls to the associated document, or to handle events that are not available on Document objects.

To generate a Document host item, use the GetVstoObject method. For more information, see Extend Word documents and Excel workbooks in VSTO Add-ins at runtime.

See also