Developer Documentation Library > UNICOM Intelligence Function Library > Miscellaneous functions > CreateObject
 
CreateObject
Creates and returns a reference to an Automation object.
Syntax
CreateObject[(<class>)]
Parameters
<class>
Type: Text
(Optional.) The COM class name of the object to be created.
If not specified, a dynamic object is created. The dynamic object initially has no properties. Its properties are defined dynamically at run-time and accessed by using a _Properties property. This properties collection keeps properties in the order added. See also The dynamic object.
(return)
Type: Object
The created object.
Notes
If the object cannot be created, an error occurs.
The dynamic object
This dynamic object maintains properties in the order in which they were set or deserialized. For the dynamic object created by CreateObject(), properties can be set by using:
MyObject.PropName = PropValue
MyObject["PropName"] = PropValue
MyObject.Item["PropName"] = PropValue
MyObject._Properties.AddNew("PropName", PropValue)
MyObject._Properties["PropName"] = PropValue
MyObject._Properties.Item["PropName"] = PropValue
The same methods can be used to read back the properties again. It is possible to enumerate the properties (as objects which have a name and value) by using MyObject or MyObject._Properties. The MyObject._Properties collection can be used to manipulate the set of properties: that is, add, remove, clear, enumerate, or index properties.
Example using a Class name
The following mrScriptBasic example uses the CreateObject function as follows:
Line 5: The CreateObject function is used to create a DataLinkHelper object so that the DisplayWizard method can be used to display the Data Link Properties dialog. For more information, see Displaying the Data Link Properties dialog.
Line 12: The CreateObject function is used to create an ADO Connection object, which is then used to open a connection to the data source and to return a recordset by executing a query against VDATA.
Line 17: The CreateObject function is used to create a Word Application object, so that the recordset can be inserted into a Word document.
Dim Wizard, ConnectionString, wordApp, wordDoc

' Use the Display wizard to open the Data Link Properties
' dialog box to get the connection string
Set Wizard = CreateObject("mrOleDB.DataLinkHelper") ' Line 5
ConnectionString = Wizard.DisplayWizard()
If Len(ConnectionString) > 0 Then
' Create a connection and use it to create a recordset
' by executing a query against VDATA
Dim adoConnection, adoRS
Set adoConnection = CreateObject("ADODB.Connection") ' Line 12
adoConnection.Open(ConnectionString)
Set adoRS = adoConnection.Execute("SELECT age, gender FROM VDATA WHERE Serial < 10")

' Copy the recordset into Word
Set wordApp = CreateObject("Word.Application") ' Line 17
wordApp.Visible = true
Set wordDoc = wordApp.Documents.Add()
wordDoc.ActiveWindow.Selection.Text = adoRS.GetString()
End If
Example using the dynamic object
Dim Style
Set Style = CreateObject()
Style.TemplateName = "PeopleWorking"
Style.Color = "Red"
Style.Logo = "PeopleWorkingRed.png"
Style.Font = CreateObject()
Style.Font.Face = "Arial"
Style.Font.Size = 12
Style.Font.Italics = True
RecurseProperties(Style)

' Properties accessed via the "_Properties" collection object
Debug.Log("The Style object has " + CText(Style._Properties.Count) + " properties")
Style._Properties.Remove("Color")
Style._Properties.Clear()
Style._Properties.AddNew("Color", "Red")

' Full syntax to access the value of a property
Debug.Log("Color: " + Style._Properties["Color"].Value)

' Short syntax to access the value of a property
Debug.Log("Color: " + Style.Color)

Sub RecurseProperties(Properties)
Dim Property
For Each Property In Properties
If (VarTypeName(Property.Value) = "Object") Then
Debug.Log(Property.Name + " = Collection")
RecurseProperties(Property.Value)
Else
Debug.Log(Property.Name + " = " + CText(Property.Value))
End If
Next
End Sub
See also
IIf
Miscellaneous functions