Udostępnij za pośrednictwem


Workbook Host Item

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

Applies to: The information in this topic applies to document-level projects and application-level projects for Excel 2007 and Excel 2010. 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 application-level projects, you can generate Workbook host items at run time.

Understanding 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 Programming 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 Microsoft.Office.Interop.Excel.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 Microsoft.Office.Interop.Excel.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: 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.

Understanding Workbook Host Items in Application-Level Projects

In application-level 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 Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time.

See Also

Concepts

Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time

Host Items and Host Controls Overview

Worksheet Host Item

Automating Excel by Using Extended Objects

Programmatic Limitations of Host Items and Host Controls

Other Resources

Office Development Samples and Walkthroughs