Adding Host Items in Excel

A VSTO solution can contain only one workbook, and the workbook that is created in a VSTO solution is automatically created as a host item. When you add a worksheet or chartsheet to a workbook at design time when Excel is hosted inside Visual Studio, VSTO automatically creates the worksheet or chartsheet as a VSTO object (a host item). VSTO does not provide a way for you to create host controls dynamically. If you add a worksheet to the workbook programmatically by adding a sheet to the Worksheets collection, it is created as an Interop worksheet rather than a worksheet host item. Keep in mind that you will not have any of the VSTO-specific functionality—such as data binding capabilities and additional events—of a worksheet that is added programmatically in this way. You also cannot add a host control or Windows Forms control to an Interop worksheet. Only VSTO worksheets can host these types of controls.

If you look at the code that is generated in the hidden partial class when you add a worksheet to the workbook at design time, you can probably figure out what code is necessary to create the worksheet host item at run time. Because this is not a supported scenario in VSTO, it is not discussed here.

0 0

Post a comment