Skip to content

Latest commit

 

History

History
173 lines (116 loc) · 14.7 KB

host-items-and-host-controls-overview.md

File metadata and controls

173 lines (116 loc) · 14.7 KB
title description ms.date ms.topic dev_langs helpviewer_keywords author ms.author manager ms.subservice
Host items and host controls overview
Learn that host items and host controls are types that help provide the programming model for Office solutions.
02/02/2017
conceptual
VB
CSharp
host controls [Office development in Visual Studio], adding
Office documents [Office development in Visual Studio, host controls
host items [Office development in Visual Studio]
application development [Office development in Visual Studio], host items
Office applications [Office development in Visual Studio], host items
host controls [Office development in Visual Studio], listed
Excel [Office development in Visual Studio], host items
host controls [Office Development in Visual Stuio], naming
host controls [Office development in Visual Studio]
host controls [Office development in Visual Studio], about host controls
document-level customizations [Office development in Visual Studio], host controls
Office applications [Office development in Visual Studio], host controls
documents [Office development in Visual Studio], host controls
controls [Office development in Visual Studio], host controls
application development [Office development in Visual Studio], host controls
Excel [Office development in Visual Studio], host controls
host items [Office development in Visual Studio], about host items
host items [Office development in Visual Studio], listed
documents [Office development in Visual Studio], host items
data binding [Office development in Visual Studio], host controls
Office documents [Office development in Visual Studio, host items
Word [Office development in Visual Studio], host items
document-level customizations [Office development in Visual Studio], host items
Word [Office development in Visual Studio], host controls
host controls [Office development in Visual Studio], deleting
John-Hart
johnhart
mijacobs
office-development

Host items and host controls overview

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.

[!INCLUDEappliesto_controls]

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:

  • xref:Microsoft.Office.Tools.Word.Document

  • xref:Microsoft.Office.Tools.Excel.Workbook

  • xref:Microsoft.Office.Tools.Excel.Worksheet

  • xref:Microsoft.Office.Tools.Excel.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 xref:Microsoft.Office.Tools.Word.Document host item extends the xref:Microsoft.Office.Interop.Word.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 xref:Microsoft.Office.Tools.Word.Document and xref:Microsoft.Office.Tools.Excel.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 xref:Microsoft.Office.Tools.Excel.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 xref:System.Data.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 xref:Microsoft.Office.Tools.Word.Document, xref:Microsoft.Office.Tools.Excel.Workbook, and xref:Microsoft.Office.Tools.Excel.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 xref:Microsoft.Office.Interop.Excel.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 xref:Microsoft.Office.Interop.Excel.Range object in Excel, you must first handle the change event of the worksheet. Then you must determine whether the change occurred within the xref:Microsoft.Office.Interop.Excel.Range. In contrast, the xref:Microsoft.Office.Tools.Excel.NamedRange host control has a xref:Microsoft.Office.Tools.Excel.NamedRange.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 xref:Microsoft.Office.Tools.Excel.NamedRange control on a xref:Microsoft.Office.Tools.Excel.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 xref:Microsoft.Office.Tools.Excel.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 xref:Microsoft.Office.Tools.Excel.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 xref:Microsoft.Office.Tools.Excel.NamedRange.Delete%2A method of a xref:Microsoft.Office.Tools.Excel.NamedRange only successfully deletes the xref:Microsoft.Office.Tools.Excel.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 xref:Microsoft.Office.Tools.Excel.Worksheet.Controls%2A or xref:Microsoft.Office.Tools.Word.Document.Controls%2A 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 xref:Microsoft.Office.Interop.Excel.Range object in Excel and xref:Microsoft.Office.Interop.Word.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 xref:Microsoft.Office.Interop.Excel._Application.EnableEvents%2A property of the xref:Microsoft.Office.Interop.Excel.Application object in Excel to false. Setting this property to false prevents Excel from raising any events, including the events of host controls.

Related content