VBScript - Calling Routines and Variables Declared in Another Unit

Applies to TestComplete 15.45, last modified on December 01, 2022

This topic describes methods of calling items declared in another unit with VBScript code.

Adding Script References

To reference another unit from the current unit using VBScript, add the following line at the beginning of the current script (before any functions and variable declarations):

VBScript

' To reference one unit
' [UnitA]
'USEUNIT UnitName

' To reference multiple units
' [UnitA]
'USEUNIT UnitName1
'USEUNIT UnitName2

There must be no spaces at the beginning of the line before the ' characters, and between these characters and the USEUNIT keyword.

Calling Items From Another Unit

Once an external unit is linked to the current one, all the routines, as well as global variables and constants of the external unit, can be called in the current unit. It is recommended, though not obligatory, to prefix the names of external routines, variables and constants with the unit's name.

That is, in general, calling items from another unit with any of the following code instructions is correct:

VBScript

Call MyExtUnit.MyRoutine(Param1, Param2)
Call MyRoutine(Param1, Param2)

MyExtUnit.MyVariable
MyVariable

MyExtUnit.MyConstant
MyConstant

You must specify the name of the referenced unit in the following cases:

  • When the current unit already contains a routine, global variable or global constant with the same name. Without prefixing, the routine, variable or constant of the current unit will be called.

  • When the name of an external routine is the same as the name of an external unit. Calling such a routine without a prefix will cause an exception.

  • When calling routines, variables or constants declared in a third unit.

To call a routine defined in an external unit that is linked to your current unit, you can also pass a string that specifies the target routine’s full name (that is, "unit_name.routine_name") to the Eval function or Execute statement. If the external routine you want to call takes parameters, include them into the string parameter you pass to the eval functions.

VBScript

' [UnitA]

'USEUNIT UnitB
Sub Test
 Execute "UnitB.TestSub"
 Execute "Call UnitB.TestSub(1, ""string"", True)"
 result = Eval("UnitB.TestFunction")
End Sub

Important Notes

  • To use VBScript classes from referenced units, you need to create a helper routine for them. For details, see VBScript - Specifics of Usage.

  • To make a variable or constant visible to other units, declare it as global, that is, outside any routines.

  • Routines and variables can be defined as Private. Private routines and variables are visible only within the unit they are declared in. They cannot be accessed from other units.

  • If the current unit is linked to a unit that is linked to another unit, the script declared in the third unit can be called from the current unit. For instance, if you need to call a function declared in UnitA from UnitC, which is linked to UnitB, you must add references to UnitA in UnitB. They will interact in the following way:

    UnitA (contains no references)UnitB (contains a reference to unitA)UnitC (contains a reference to unitB)

    If the references are correct, the items from UnitA will be available in UnitC, like in the following example:

    VBScript

    ' [UnitA]
    Function TestProcedure()
      Log.Message "The procedure is run."
    End Function
    ' ...
      
    ' [UnitB]
    'USEUNIT UnitA
    ' ...
      
    ' [UnitC]
    'USEUNIT UnitB
    Sub Test2
      UnitA.testProcedure
    End Sub
    ' ...

    Scripts declared in a fourth unit and greater cannot be called in the current unit using VBScript.
  • VBScript allows script units to refer to each other with circular references. For instance, if you need to call scripts declared in UnitA from UnitB, and to call scripts declared in UnitB from UnitA, you must add references to both units.

  • The USEUNIT statement does not copy, share, or merge project items. These elements remain available only for the project the unit is imported from. To be able to work with elements that reside within external projects, import them to or share them with the current project. For more information about this, see Sharing Project Items and Their Child Elements Among Several Projects.

  • If you reference a script from another project, and that script uses object names from Name Mapping, you may need to merge the external Name Mapping file with your local Name Mapping file. For instructions, see Merge Name Mapping Files.

See Also

Calling Routines and Variables Declared in Another Unit from GUI
Calling Routines Declared in Another Project
Supported Scripting Languages - Specifics of Usage

Highlight search results