When you create a new application-level project for Excel, 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, 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 Basic | C# |
|---|
ThisWorkbook.vb | ThisWorkbook.cs |
Sheet1.vb | Sheet1.cs |
Sheet2.vb | Sheet2.cs |
Sheet3.vb | Sheet3.cs |
You can use the Globals class in your project to access ThisWorkbook, Sheet1, Sheet2, or Sheet3 from outside of the respective class. For more information, see Global Access to Objects in Office Projects. The following example calls the PrintPreview method of Sheet1 regardless of whether the code is placed in one of the Sheetn classes or the ThisWorkbook class.
Globals.Sheet1.PrintPreview()
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 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 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.
Note |
|---|
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
Worksheet Object
Range Object