Example: Creating an Excel Object Using Late Binding

The code sample below illustrates how you would use late binding to create an Excel object in an ATEasy program. Since the application does not reference the information contained in Excel's object library, the Excel object variables are declared as type Object:

 

Variable

Declared as Type:

ob

Object

obRange

Object

obChart

Object

Because these variables are declared as the generic type Object, the code that uses them must communicate more with Excel at run time, and performance suffers compared to early bound code that uses specific variable declarations.

! create excel app object

ob=CreateObject("Excel.Application")

if ob=Nothing

MsgBox("Unable to Create Excel.Application. Check if MS Excel is intsalled properly. Aborting...")

abort

endif

ob.Caption="ATEasy Excel Demo Using COM"

ob.Visible=TRUE     ! show/activate execl main window

ob.Workbooks.Add()  ! add workbook

! prepare data

as={"US-West", "US-East", "US-Central", "Europe", "Israel"}

ad={2123300.00, 2323300.00, 1123300.00, 1523300.00, 1200000.00}

iSize=sizeof(as)/sizeof(as[0])

! fill cells

for i=1 to iSize

ob.Cells(i, 1).Value = as[i-1]

ob.Cells(i, 2).Value = ad[i-1]

next

! check if cells got the data

if ob.Cells(1, 1).Value<>as[0]

TestStatus=FAIL

endif

! add a new workbook with a chart

ob.Workbooks.Add()              ! add workbook for chart

ob.Windows.Arrange()            ! cascade the two workbooks

obChart=ob.Charts.Add()         ! add a chart to the workbook

obRange=ob.Workbooks(1).Sheets(1).Range("A1:B5")  ! get the range for the chart

obChart.SetSourceData(obRange)  ! fill\plot the chart

! free the objects

obRange=Nothing

obChart=Nothing

ob=Nothing