
Understanding the Document Host Item in Document-Level Projects
To programmatically access the document in your project, use the ThisDocument class. When you create a document-level project, Visual Studio Tools for Office generates the ThisDocument class to serve as the communication link between Word and your customization code. The ThisDocument class derives from the Document host item class. Visual Studio Tools for Office automatically instantiates the ThisDocument class for you when the document is opened at run time. You should not try to instantiate the Document class directly in your 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 and smart tags 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 Microsoft.Office.Interop.Word..::.Document class 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
A document-level project can contain only one Document host item. You cannot add new Document host items to your project at design time, and you cannot programmatically 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 Microsoft.Office.Interop.Word..::.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: Create New Documents.