0

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.

1
  • 1
    No, there isn't .Application.Workbooks.Add is the same as .Workbooks.Add, only longer. Commented Feb 25, 2013 at 18:50

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.