Thursday, February 28, 2008

How is CreateObject different from GetObject?

One of my junior colleague had asked the question "What is the difference between CreateObject and GetObject and in what situation GetObject is to be used?". This post is the answer that I have given him.

  1. CreateObject creates an instance of a class
  2. GetObject gets the reference of an running object to another object
  3. Always use CreateObject except for Windows Management Instrumentation (WMI) and Active Directory Service Interfaces (ADSI)

The below script demonstrates the use of these two VBScript functions.

Set objExcel = CreateObject("Excel.Application") 
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add

Set objExcel2 = GetObject(, "Excel.Application")
Set objWorkbook = objExcel2.Workbooks(1)
Set objWorksheet = objWorkbook.Worksheets(1)
objExcel2.Cells(1, 1).Value = "In god I TRUST everything else I TEST."



 



-- Lakshminarasimha Manjunatha Mohan


In God I TRUST everything else I TEST.

2 comments:

ranjan singh said...

sir i currently working as automation engineer in delhi. ur scripts are very good and i follow ur blog regularly. sir i m having certain issue regarding QTP automation. sir my mail id is ranjansingh83@gmail.com, sir i need ur valuable suggestion for that ..

Anonymous said...

Good information!