Creates and returns a reference to an OLE object.
Set objectvariable = CreateObject("progID", ["servername"])
You can use the CreateObject function in a Set statement to create a new object and assign an object reference to an object variable. You must specify the object's programmatic identifier as an argument to the function, and the object you want to access must be externally creatable.
The progID argument is usually the fully qualified class name of the object being created; for example, Word.Document. However, progID can be different from the class name. For example, the progID for a Microsoft Excel object is "Sheet" rather than "Worksheet." The optional servername argument can be specified to create an object on a remote machine across a network. It takes the Machine Name portion of a share name. For example, with a network share named \\MyServer\Public, the servername argument would be "MyServer."
The following code example starts Microsoft Excel (if Microsoft Excel is not already running) and establishes the variable xlApp to refer to an object of the Application class. The argument "Excel.Application" fully qualifies Application as a class defined by Microsoft Excel:
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
' Declare an object variable to hold the object
' reference. Dim as Object causes late binding.
Dim ExcelSheet As Object
Set ExcelSheet = CreateObject("Excel.Sheet")
' Make Excel visible through the Application object.
ExcelSheet.Application.Visible = True
' Place some text in the first cell of the sheet.
ExcelSheet.Worksheets("Sheet1").Range("A1").Value = "This is column A, row 1"
' Save the sheet to test.xls in the application directory.
ExcelSheet.SaveAs App.Path & "\TEST.xls"
' Close Excel with the Quit method on the Application object.
ExcelSheet.Application.Quit
' Release the object variable.
Set ExcelSheet = Nothing
' Tidy up line
Kill App.Path & "\TEST.xls"
This code starts the application creating the object, in this case, a Microsoft Excel spreadsheet. Once an object is created, you reference it in code using the object variable you defined. Then, you access properties and methods of the new object using the object variable, ExcelSheet, and other Microsoft Excel objects, including the Application object and the Cells collection.
For those who do not have Microsoft Excel, the following example invokes an instance of the ubiquitous Internet Explorer:
// Dim a generic object variable
Dim ie As Object
// Assign a new occurence of Internet Explorer to the object
Set ie = CreateObject("InternetExplorer.Application")
// Use IE's in-built APIs to manipulate the object
ie.navigate "http://www.gfabasic32.blogspot.co.uk/"
ie.visible = True
// Create an alternative means of closing Internet Explorer
OpenW Center 1, , , 130, 90 : Win_1.ControlBox = False
Ocx Command cmd = "Close IE", 10, 10, 100, 22
Do : Sleep : Until Win_1 Is Nothing
Sub cmd_Click
Try
ie.quit
Catch
// RPC Error - Internet Explorer already closed
EndCatch
CloseW 1
Set ie = Nothing
EndSub
{Created by Sjouke Hamstra; Last updated: 27/09/2014 by James Gaite}