Microsoft Office Tutorials and References
In Depth Information
The workbook host item
In Excel, you have three types of host items:
Workbook
Worksheet
ChartSheet
In every Excel solution, there is one workbook host item and a separate worksheet
host item for each worksheet in the workbook. All of these host items are folded
in the native Excel workbook, Excel worksheet, and the Excel ChartSheet, which
together constitute the Microsoft.Office.Interop.Excel namespace.
The workbook host item
A workbook host item is created whenever you create an Excel 2007 workbook
project using Visual Studio 2008, that is, document-level customization for Microsoft
Office Excel. Visual Studio Tools for Office, by design, creates a workbook host item
within the project solution. When programming, you need to reference the workbook
using the ThisWorkbook object. The workbook host item is a workbook object model,
which is available in the VSTO Excel object model.
There are numerous events exposed in the workbook host item. You will see
some of these events in the sample code below. In this example, you're going to
see the sample work for the NewWorkbook event in an Excel workbook. First let's
see the NewWorkbook event for the workbook host item. Here, let's register the
NewWorkbook event in the workbook solution. Within Office 2007 tools such as Excel
2007 and other Office applications, there is an option to handle events by using
the Event Manager . The Event Manager provides mechanisms by which forms or
documents can provide and respond to events. The Event Manager is called from the
InternalStartup() method which in turn controls the events that are registered
when a document is initially loaded. Each captured event can be registered within
the InternalStartup() method, and then the delegates can be constructed to
encapsulate a reference to a method that handles the events' custom code.
Open Visual Studio 2008 and create a new solution, as usual. Write the following
code inside the ThisWorkbook.cs file:
private void InternalStartup()
{
// Explicitly cast the application object to the application
events
((Excel.AppEvents_Event)ThisApplication).NewWorkbook += new
Microsoft.Office.Interop.Excel.
AppEvents_NewWorkbookEventHandler(ThisWorkbook_
NewWorkbook);
 
Search JabSto ::




Custom Search