Export (0) Print
Expand All

Excel Object Model Overview

To develop solutions that use Microsoft Office Excel, you can interact with the objects provided by the Excel object model. This topic introduces the most important classes:

For the most part, the object model directly emulates the user interface. For example, the Application object represents the entire application, and each Workbook object contains a collection of Worksheet objects. From there, the major abstraction representing cells is the Range object, which allows you to work with individual cells or groups of cells.

Microsoft Visual Studio 2005 Tools for the Microsoft Office System (VSTO 2005) extends many of these native objects into host items and host controls that can be used in document-level customizations. These controls have additional functionality, including data binding capabilities and events. For example, a native Excel Microsoft.Office.Interop.Excel.Range object is extended into a Microsoft.Office.Tools.Excel.NamedRange control, which can be bound to data and exposes events. For more information about host items and host controls, see Host Items and Host Controls Overview.

For complete information about the Excel 2003 object model, see the VBA documentation that is installed with Excel or see "Welcome to the Microsoft Office Excel 2003 VBA Language Reference" (http://go.microsoft.com/fwlink/?linkid=27951). For complete information about the Excel 2007 object model, see the VBA documentation that is installed with Excel or see the 2007 Microsoft Office system developer content on the MSDN Web site (http://go.microsoft.com/fwlink/?LinkId=72870).

Accessing Objects in an Excel Project

When you create a new application-level project for Excel by using Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System (VSTO 2005 SE), Visual Studio automatically creates a ThisAddIn.vb or ThisAddIn.cs code file. You can access the Application object by using Me.Application or this.Application.

When you create a new document-level project for Excel by using VSTO 2005, you have the option of creating a new Excel Application or Excel Template project. VSTO 2005 automatically creates the following code files in your new Excel project for both document and template projects.

Visual Basic C#

ThisWorkbook.vb

ThisWorkbook.cs

Sheet1.vb

Sheet1.cs

Sheet2.vb

Sheet2.cs

Sheet3.vb

Sheet3.cs

You can use the global class, Globals, to access ThisWorkbook, Sheet1, Sheet2, or Sheet3 from outside of the respective class. For more information, see Global Access to Objects in the Project. The following example calls the PrintPreview method of Sheet1 regardless of whether the code is placed in the Sheet class or the Workbook class:

Globals.Sheet1.PrintPreview(missing);

Because the data in an Excel document is highly structured, the object model is hierarchical and straightforward. Excel provides hundreds of objects with which you might want to interact, but you can get a good start on the object model by focusing on a very small subset of the available objects. These include:

  • Application Object

  • Workbook Object

  • Worksheet Object

  • Range Object

Much of the work done with Excel centers around these four classes and their members.

Application Object

The Excel Application object represents the Excel application itself. The Application object exposes a great deal of information about the running application, the options applied to that instance, and the current user objects open within the instance.

NoteNote

You should not set the EnableEvents property of the Application object in Excel to false. Setting this property to false prevents Excel from raising any events, including the events of host controls.

Workbook Object

The Microsoft.Office.Interop.Excel.Workbook class represents a single workbook within the Excel application. Visual Studio Tools for Office extends the Microsoft.Office.Interop.Excel.Workbook class by providing the Microsoft.Office.Tools.Excel.Workbook class, which gives you access to all members of the Workbooks collection, as well as data binding capabilities and additional events.

Worksheet Object

The Microsoft.Office.Interop.Excel.Worksheet object is a member of the Worksheets collection. Many of the properties, methods, and events of the Microsoft.Office.Interop.Excel.Worksheet are identical or similar to members provided by the Application or Microsoft.Office.Interop.Excel.Workbook classes.

Excel provides a Sheets collection as a property of a Microsoft.Office.Interop.Excel.Workbook object, but there is no Sheet class in Excel. Instead, each member of the Sheets collection is either a Microsoft.Office.Interop.Excel.Worksheet, or a Microsoft.Office.Interop.Excel.Chart object. Visual Studio Tools for Office provides a Microsoft.Office.Tools.Excel.Worksheet host item and creates three instances: Sheet1, Sheet2, and Sheet3, upon project creation. Access to any of the sheets can be made through the Globals reference. Visual Studio Tools for Office extends the Microsoft.Office.Interop.Excel.Worksheet object, providing the Microsoft.Office.Tools.Excel.Worksheet host item.

Range Object

The Microsoft.Office.Interop.Excel.Range object is the object you will use most within your Excel applications. Before you can manipulate any region within Excel, you must express it as a Range object and work with methods and properties of that range. A Range object represents a cell, a row, a column, a selection of cells containing one or more blocks of cells (which might or might not be contiguous), or even a group of cells on multiple sheets.

Visual Studio Tools for Office introduces two range host controls: the Microsoft.Office.Tools.Excel.NamedRange control and the Microsoft.Office.Tools.Excel.XMLMappedRange control. For more information on host controls, see Host Items and Host Controls Overview.

Extended Objects in Document-Level Projects

It is important to understand the differences between the native objects provided by the Excel object model and the extended objects (host items and host controls) provided by VSTO 2005, because both types of objects are available to document-level projects. Some points to keep in mind include:

  • Design time. When you add any of the extended Excel objects at design time, they are automatically created as host items and host controls. For example, if you add a list to a worksheet in the Designer, code is automatically generated to extend the list into a Microsoft.Office.Tools.Excel.ListObject control.

  • Run time. Host items are not automatically created at run time. If you add workbooks, worksheets, or chart sheets at run time, they are native Excel objects and do not have the additional capabilities that host items provide. You can programmatically add many of the host controls. For more information, see Host Items and Host Controls Overview.

  • Data binding and events. Host items and host controls have data-binding capabilities and events, which are not available to the native objects.

  • Types. The native Excel objects use the types defined in the Microsoft.Office.Interop.Excel namespace, whereas host items and host controls use the aggregated types defined in the Microsoft.Office.Tools.Excel namespace.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft