pushkar7767
pushkar7767

Reputation: 21

Application object in microsoft excel object model

Today while practicing vb script i came up with a little doubt please help to clear my doubt. I have written this code and created a excel sheet in my D: drive

dim excel
SET excel=CreateObject("Excel.Application")
excel.Visible=true
excel.application.Workbooks.Add
excel.ActiveWorkbook.SaveAs"D:\pushkar23.xls"
excel.quit
SET excel=nothing

But if I change the line

excel.application.Workbooks.Add 

to

excel.Workbooks.Add

then still the sheet is created in my D: drive.
Please tell me if there is any difference between both methods.

Upvotes: 0

Views: 2716

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

If you look at the docs, you'll see that the Application object (and some others, e.g. Workbook) has an Application property that

Returns an Application object that represents the creator of the specified object

It's always the same:

Option Explicit

Dim oExcel : Set oExcel = CreateObject("Excel.Application")
WScript.Echo 0, CStr(oExcel Is oExcel.Application)
WScript.Echo 1, CStr(oExcel Is oExcel.Application.Application.Application)
Dim oWB : Set oWB = oExcel.Application.Application.Workbooks.Add
WScript.Echo 2, CStr(oWB.Application Is oExcel.Application)
oExcel.Quit

output:

0 True
1 True
2 True

For efficency and clarity, however, you should use plain oExcel wherever possible.

Upvotes: 1

Related Questions