Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Excel object model overview

Feedback

In this article

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 objects:

Note

Interested in developing solutions that extend the Office experience across multiple platforms? Check out the newOffice Add-ins model. Office Add-ins have a small footprint compared to VSTO Add-ins and solutions, and you can build them by using almost any web programming technology, such as HTML5, JavaScript, CSS3, and XML.

The object model closely follows the user interface. TheApplication object represents the entire application, and eachWorkbook object contains a collection ofWorksheet objects. From there, the major abstraction that represents cells is theRange object, which enables you to work with individual cells or groups of cells.

In addition to the Excel object model, Office projects in Visual Studio providehost items andhost controls that extend some objects in the Excel object model. Host items and host controls behave like the Excel objects they extend, but they also have additional functionality such as data-binding capabilities and extra events. For more information, seeAutomate Excel by using extended objects andHost items and host controls overview.

This topic provides a brief overview of the Excel object model. For resources where you can learn more about the entire Excel object model, seeUse the Excel object model documentation.

Access objects in an Excel project

When you create a new VSTO Add-in project for Excel, Visual Studio automatically creates aThisAddIn.vb orThisAddIn.cs code file. You can access the Application object by usingMe.Application orthis.Application.

When you create a new document-level project for Excel, you have the option of creating a new Excel Workbook or Excel Template project. Visual Studio automatically creates the following code files in your new Excel project for both workbook and template projects.

Visual BasicC#
ThisWorkbook.vbThisWorkbook.cs
Sheet1.vbSheet1.cs
Sheet2.vbSheet2.cs
Sheet3.vbSheet3.cs

You can use theGlobals class in your project to accessThisWorkbook,Sheet1,Sheet2, orSheet3 from outside of the respective class. For more information, seeGlobal access to objects in Office projects. The following example calls thePrintPreview method ofSheet1 regardless of whether the code is placed in one of theSheetn classes or theThisWorkbook class.

Globals.Sheet1.PrintPreview();

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 small subset of the available objects. These objects include the following four:

  • Application

  • Workbook

  • Worksheet

  • Range

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

Application object

The ExcelApplication object represents the Excel application itself. TheApplication 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.

Note

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

Workbook object

TheWorkbook object represents a single workbook within the Excel application.

The Office development tools in Visual Studio extend theWorkbook object by providing theWorkbook type. This type gives you access to all features of aWorkbook object. For more information, seeWorkbook host item.

Worksheet object

TheWorksheet object is a member of theWorksheets collection. Many of the properties, methods, and events of theWorksheet are identical or similar to members provided by theApplication orWorkbook objects.

Excel provides aSheets collection as a property of aWorkbook object. Each member of theSheets collection is either aWorksheet or aChart object.

The Office development tools in Visual Studio extend theWorksheet object by providing theWorksheet type. This type gives you access to all features of aWorksheet object, as well as new features such as the ability to host managed controls and handle new events. For more information, seeWorksheet host item.

Range object

TheRange 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 aRange object and work with methods and properties of that range. ARange object represents a cell, a row, a column, a selection of cells that contains one or more blocks of cells, which might or might not be contiguous, or even a group of cells on multiple sheets.

Visual Studio extends theRange object by providing theNamedRange andXmlMappedRange types. These types have most of the same features as aRange object, as well as new features such as the data binding capability and new events. For more information, seeNamedRange control andXmlMappedRange control.

Use the Excel object model documentation

For complete information about the Excel object model, you can refer to the Excel primary interop assembly (PIA) reference and the VBA object model reference.

Primary interop assembly reference

The Excel PIA reference documentation describes the types in the primary interop assembly for Excel. This documentation is available from the following location:Excel 2010 primary interop assembly reference.

For more information about the design of the Excel PIA, such as the differences between classes and interfaces in the PIA and how events in the PIA are implemented, seeOverview of classes and interfaces in the Office primary interop assemblies.

VBA object model reference

The VBA object model reference documents the Excel object model as it is exposed to Visual Basic for Applications (VBA) code. For more information, seeExcel 2010 object model reference.

All of the objects and members in the VBA object model reference correspond to types and members in the Excel PIA. For example, the Worksheet object in the VBA object model reference corresponds to theWorksheet object in the Excel PIA. Although the VBA object model reference provides code examples for most properties, methods, and events, you must translate the VBA code in this reference to Visual Basic or Visual C# if you want to use them in an Excel project that you create by using Visual Studio.

Related topics

TitleDescription
Excel solutionsExplains how you can create document-level customizations and VSTO Add-ins for Microsoft Office Excel.
Work with rangesProvides examples that show how to perform common tasks with ranges.
Work with worksheetsProvides examples that show how to perform common tasks with worksheets.
Work with workbooksProvides examples that show how to perform common tasks with workbooks.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?