Partager via


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 application-level projects for Word 2013 and Word 2010. 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 application-level projects, you can generate Document host items at run time.

Understanding 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 Programming 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 run time from a document-level customization.

If you create a new Word document at run time, 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.

Understanding Document Host Items in Application-Level Projects

In application-level 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 Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time.

See Also

Concepts

Host Items and Host Controls Overview

Automating Word by Using Extended Objects

Programmatic Limitations of Host Items and Host Controls

Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time

Other Resources

Word Object Model Overview