Host items and host controls overview

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Host items and host controls are types that help provide the programming model for Office solutions that are created by using the Office development tools in Visual Studio. Host items and host controls make interacting with the object models of Microsoft Office Word and Microsoft Office Excel, which are based on COM, more like interacting with managed objects such as Windows Forms controls.

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

Host items

Host items are types that are at the top of object model hierarchies in Office projects. The Visual Studio Tools for Office runtime defines the following host items for Word and Excel solutions:

  • Document

  • Workbook

  • Worksheet

  • ChartSheet

    Each of these types extends an object that exists natively in the Word or Excel object model, called a native Office object. For example, the Document host item extends the Document object, which is defined in the primary interop assembly for Word.

    Host items generally have the same base functionality as the corresponding Office objects, but are enhanced with the following features:

  • The ability to host managed controls, including host controls and Windows Forms controls.

  • Richer event models. Some document, workbook, and worksheet events in the native Word and Excel object models are raised only at the application level. Host items provide these events at the document level, so that it is easier to handle the events for a specific document.

Understand host items in document-level projects

In document-level projects, host items provide an entry point for your code, and they have designers that help you develop your solution.

The Document and Worksheet host items have associated designers that are the visual representation of the document or worksheet, like a Windows Forms designer. You can use this designer to modify the content of the document or worksheet directly in Word or Excel, and to drag controls onto the design surface. For more information, see Document host item and Worksheet host item.

The Workbook host item does not act as a container for controls that have a user interface. Instead, the designer for this host item functions as a component tray, which enables you to drag a component, such as a DataSet, onto its design surface. For more information, see Workbook host item.

Host items cannot be created programmatically in document-level projects. Instead, use the ThisDocument, ThisWorkbook, or Sheetn classes that Visual Studio automatically generates in your project at design time. These generated classes derive from the host items, and they provide an entry point for your code. For more information, see Programmatic limitations of host items and host controls.

Understand host items in VSTO Add-in projects

When you create a VSTO Add-in, you do not have access to any host items by default. However, you can generate Document, Workbook, and Worksheet host items in Word and Excel VSTO Add-ins at run time.

After you generate a host item, you can perform tasks such as adding controls to documents. For more information, see Extend Word documents and Excel workbooks in VSTO Add-ins at run time.

Host controls

Host controls extend various user interface (UI) objects in the Word and Excel object models, such as Microsoft.Office.Interop.Word.ContentControl and Range objects.

The following host controls are available for Excel projects:

  • Chart control

  • ListObject control

  • NamedRange control

  • XmlMappedRange control

    The following host controls are available for Word projects:

  • Bookmark control

  • Content controls

  • XMLNode control

  • XMLNodes control

    Host controls that are added to Office documents behave like the native Office objects; however, host controls have additional functionality, including events and data-binding capabilities. For example, when you want to capture the events of a native Range object in Excel, you must first handle the change event of the worksheet. Then you must determine whether the change occurred within the Range. In contrast, the NamedRange host control has a Change event that you can handle directly.

    The relationship between a host item and host controls is similar to the relationship between a Windows Form and Windows Forms controls. Just as you would place a text box control on a Windows Form, you place a NamedRange control on a Worksheet host item. The following illustration shows the relationship between host items and host controls.

    Relationship between host items and host controls

    You can also use Windows Forms controls in your Office solutions by adding them directly to the Word and Excel document surface. For more information, see Windows Forms controls on Office documents overview.

Note

Adding host controls or Windows Forms controls to a Word subdocument is not supported.

Add host controls to your documents

In document-level projects, you can add host controls to your Word documents or Excel worksheets at design time in the following ways:

Name host controls

When you drag a host control from the Toolbox to your document, the control is automatically named using the control type with an incremental number at the end. For example, bookmarks are named bookmark1, bookmark2, and so on. If you use the native functionality of Word or Excel to add the control, you can give it a specific name at the time that you create it. You can also rename your controls by changing the value of the Name property in the Properties window.

Note

You cannot use reserved words to name host controls. For example, if you add a NamedRange control to a worksheet and change the name to System, errors occur when you build the project.

Delete host controls

In document-level projects, you can delete host controls at design time by selecting the control on the Excel worksheet or Word document and pressing the Delete key. However, you must use the Define Name dialog box in Excel to delete NamedRange controls.

If you add a host control to a document at design time, you should not remove it programmatically at run time because the next time you try to use the control in code, an exception is thrown. The Delete method of a host control only removes host controls that are added to the document at run time. If you call the Delete method of a host control that was created at design time, an exception is thrown.

For example, the Delete method of a NamedRange only successfully deletes the NamedRange if it was programmatically added to the worksheet, which is known as creating host controls dynamically. Dynamically created host controls can also be removed by passing the control name to the Remove method of the Controls or Controls property. For more information, see Add controls to Office documents at run time.

If end users delete a host control from the document at run time, the solution might fail in unexpected ways. You can use the document protection features in Word and Excel to protect the host controls from being deleted. For more information, see Office development samples and walkthroughs.

Note

Do not programmatically remove controls during the Shutdown event handler of the document or worksheet. The UI elements are no longer available when the Shutdown event occurs. If you want to remove controls before the application closes, add your code to another event handler such as BeforeClose or BeforeSave.

Program against host control events

One way that host controls extend Office objects is by adding events. For example, the Range object in Excel and Bookmark object in Word do not have events, but the Visual Studio Tools for Office runtime extends these objects by adding programmable events. You can access and code against these events the same way you access events of controls on Windows Forms: through the event drop-down list in Visual Basic and the event property page in C#. For more information, see Walkthrough: Program against events of a NamedRange control.

Note

You should not set the EnableEvents property of the Application object in Excel to false. Setting this property to false prevents Excel from raising any events, including the events of host controls.

See also