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.
- CreateObject creates an instance of a class
- GetObject gets the reference of an running object to another object
- 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.