다음을 통해 공유


Workbook host item

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

The Workbook host item is a type that extends the Workbook type from the primary interop assembly for Excel. The Workbook host item provides all of the same properties, methods, and events as a Workbook object, but it also provides additional features.

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

In document-level projects, there is a default Workbook host item that represents the workbook in your project. In VSTO Add-in projects, you can generate Workbook host items at run time.

Understand the workbook host item in document-level projects

To access the workbook in your project, use the ThisWorkbook class. The ThisWorkbook class gives you access to members of the Workbook host item to perform basic tasks in your customization, such as running code when the workbook is opened or closed. For more information, see Program document-level customizations.

The ThisWorkbook 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 Workbook object in the primary interop assembly for Excel, you can also use ThisWorkbook to access the object model of Excel. For more information, see Excel object model overview.

Double-click the ThisWorkbook project item in Solution Explorer to display the workbook designer and to view the properties and events of the workbook in the Properties window.

Limitations of the workbook host item in document-level projects

A document-level project can contain only one Workbook host item (that is, the ThisWorkbook class). You cannot add new Workbook host items to your project at design time, and you cannot create new Workbook host items at run time from a document-level customization.

If you create a new Excel workbook at run time, it will be of the type Workbook. Because it is not a host item, it cannot contain any host controls or Windows Forms controls. For more information about creating workbooks at run time, see How to: Programmatically create new workbooks.

The Workbook host item does not act as a container for host controls. Therefore, you cannot add any visible controls to the workbook, but you can add components, such as a DataSet, so that the components can be shared by all worksheets. In a document-level project, components available to the workbook can be found on the Component tab, Data tab, and All Windows Forms tab of the Toolbox.

Note

The Office development tools in Visual Studio do not support shared workbooks.

Understand workbook host items in VSTO Add-in projects

In VSTO Add-in projects, you can generate a Workbook host item at run time for any workbook that is open in Excel. To generate a Workbook host item, use the GetVstoObject method. For more information, see Extend Word documents and Excel workbooks in VSTO Add-ins at run time.

See also