Declaration of OLE Automation Objects
- Last UpdatedJul 18, 2023
- 1 minute read
VBA objects can only be declared and referenced within VBA file modules. VBA modular objects have modular scope and cannot be referenced (accessed and used) from outside their VBA module (file).
Note: VBA objects cannot be used directly in Plant SCADA command or expression fields.
Once declared within a VBA module, VBA objects can be referenced and used in any procedure within the same code module. An object declared outside of a procedure has modular scope to all procedures within that same VBA module (file). Objects declared within a Sub or Function procedure have local scope only within that procedure.
The object variable must be declared before it can be assigned an object reference. Object variables are declared by the Dim Statement with the As Object VBA data type using the following syntax:
Dim <VariableName> As Object
Where:
-
Dim is the required Variable declaration statement BASIC keyword
-
<VariableName> represents the required name of the variable being declared (dimensioned)
-
As Object declares the variable as a VBA 'object' data type
Note: The placeholder shown inside arrow brackets (<placeholder>) should be replaced in any actual code with the value of the item that it describes. The arrow brackets and the word they contain should not be included in the statement, and are shown here only for your information.
For example:
' create local variables to store object references
Dim objExcelApp As Object
Dim objWordApp As Object
Once declared, you can then assign an OLE Automation reference to the object variable in VBA.