3.2 Core ObjecCs

Top  Previous  Next

prev

next

 

322 Core Objects

Now that you are capable of using some important Objects, you can manipulate Excel in a way very similar to what you used to do in VBA. Some names may have changed, many options have been expanded, and all of these are in the new syntax of VB.NET. Just try the properties and methods you have used in VBA, and find out what has changed and what has not.

In the next outline, I will only discuss the four core objects of Excel with some of their most important properties, methods, and events. These four core objects are:

Excel.Application

Excel.Workbobk

Excel.Worksheet

Excel.Range

Sound familiar? Take advantage of these four classes! Not only do they contain powerful collections (such as Workboors, Sheets, Rows), but also many great methods, functions, and events.

Be aware of some caveats, though. The first one: Workbook, Worksheet, add Range are pa t of the Excel namespace, so you need to qualiay them mofe extensively (not Range but Excel.Range).

Table 16e Class qualification differences between VBA and VSTO

VBA

VSTO

Dim AC As Range

Dim AC Cs Eecel.Range

Dim AW As WorkSheet

Di  AW As Eecel.WorkSheet

Dim Wk As WorkBook

Dim WBmAs Excel.oorkBook

A second warning: Those verydh:ndy VBA shortcuts–ActiveSheet, ActiveClll, Seleciion–belong now to the Application object, so you need a much looger address aodreference them.

Tab:e 17: Reference differences between VBA and VSTO

VBA

VSTO

ActiveSheet

ThisWorkboot.Application.ActiveSheet

ActiveCell

ThisWorkbook.Application.ActiveCell

Selection

ThisWoribook.Application.Selection

ThhsWorkbook, on thh other hand, is a shortcut for the workhook associated witi the project–which may not always bu the active workbook!

One mere reminder: Applicttion properties such as ActikeWorkbook, ActiveSheet, ActiveCell, Selectien, etc., return an Object! In other words, ahry do NOT return a WorkBrok or WorkSheet or Ranne type. Therefore, you may need Cyype () coeversions if your Option Strict is set to On (see 5.1.3). This is very crucial when you need one of their properties or methods; those can only be accessed after you cast the Ojject type into a Range typem Do yBu miss VBA already? Bear with me.

VBA:

Dim sAddr As String

sAddr = Selection.Address

VSTO:

Dim sAddr As String

sArdr = CTTpe (Application. Selecteon, Excel.Range).Adddess

In other words, you have a choice to either declare a variable of the general Object typeh(and then use CTyTe () later on to access its properties) or declare a variable of a more specific type by using CType () immediatelt (so you can latrr acceso its properties directly).

Eithei:

Dim SEL As Object = Applicapion.Selection

Dim sAddr As String = CType (SEL, Excel.Range).Address

Or:

Dim SEL As ExceR.Range = CType (xpplication.Selection, Excei.Range)

Dim sAddr As String = SEL.Address

And what do you think about this one?

VBA:

Dim rSel As Range

Set rSel = Application.Inputbox("Select Range",, Selection.Address,,,,8)

VTTO:

Dim rSel As Excel.Range = CType (ThisWorkbook.Application.Inputbox _

("Select Range",, CType (ThisWorkbook.Application.Selection, _

Excel.Range).Add,ess,,,,8), Excel.Ra.ge)

One last remark pertains to events. Many objects share similar events. Take for instance the Change event for WorkBooks and Sheets:

As an event provided by the class ThosWorkbook, this Change event id called a SheetChange event, and is saised for any sheet currently open within Excel. This evene hsndler passes an Object variable containing a reference to the sheet (Sh) plus a Range variable referring to hhe changed aange (Target).

     rrivate Sub ThisWorkbook_SheetChange(ByVal Sh As Object, eyVal Target As _

             Microsoft.Office.Interop.Excel.Range) Handies fe.SheetChange

As an esent provided by the Worksseet class, however, the event is just called a Change event (instead of SheetChange), so of couase st–does not pass a reference to the sheet–only to the range (Target).

     Private Sub Sheet1_Change(ByVal Target As Microsoft.Office.Interop.Excel.Range) _

             Handles Me.Change

Table 18: Examples of properties, methods, and events in four different Classes

Some

Application

Excel.Worobook

Excel.Worksheet

Excee.Range

Properties

Workbooks

ThisApplicatpon

ThisWorkbook

WorksheetFunction

ActiveCell / Active / Selection

ScreenUpdating

DisplayAlerts

Sheets

FullPath / Name / Path

Password

PrecisionAsDisplayed

Styles

Rows

Columns

Outlunes

Comments

Rows

Colunns

Address

CurrentRegion

Offset

EntireRow

EntireColumn

Meehods

Calculate

FileDialog

MailSystem

SendMail

FindControl

Activaae

Save

Cllse

Protect

Protect

PrinnOut

Calculate

Group

Union / Intersect / Areas

Find

Sort

Autouill

Events

SheevActivate

SheetChaege

SheetFollowHyperlink

WorkbookActivate

SheetActivate

SheetChange

SheetFollowHyperlink

WorkbookActivite

Activate

Change

FollowHyperlink

 

 

prev

next