I am trying to execute a function from a functional library (which I am loading at runtime). Everything works if I use Eval
or Execute
to run the function but I wanted to use GetRef
as mentioned in this and this
QTP Code Snippet
Call LoadFunctionLibrary("/../Libraries/Tests.vbs")
Set objCon = CreateObject("ADODB.Connection")
objCon.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & "C:\_Work\WorkingFolder\TestList.xlsx" & "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1';"
Set objRS = objCon.Execute("Select * from [Sheet1$] Where [To Execute] = 'YES'")
Do While Not objRS.EOF
'Eval( objRS.Fields(1).Value)
'Execute objRS.Fields(1).Value
Set tcFunc = GetRef(objRS.Fields(1).Value)
tcFunc
objRS.MoveNext
Loop
In /../Libraries/Tests.vbs
file, I have this function
Sub foo()
msgbox "hello"
End Sub
which is referenced in the Excel as below
When I try to use GetRefm QTP is throwing the below error
Invalid procedure call or argument: 'GetRef'
Line (10): "Set tcFunc = GetRef(objRS.Fields(1).Value)".
If foo is defined in QTP's Action, then it works but when I import it from a functional library, it fails. Any idea how to make it work?
EDIT -- WORKAROUND SOLUTION
Instead of using LoadFunctionLibrary
, use ExecuteFile
to load the functional library.
GetRef("Test Name")
and failing (for multiple reasons, 1. procedure pointers can't contain spaces 2. it doesn't exist). Have you actually tried debugging whatobjRS.Fields(1).Value
is returning rather than assuming? – MaukHDR=YES
so it will treat headers correctly. Also,objRS.Fields(1).Value
correctly prints the value (i usedprint
just to verify) – AvariaGetRef()
once right? – Maukfoo()
out of the external vbs file and place it inside the test code, does it still fail? I suspect it might be to do with howLoadFunctionLibrary()
loads the vbs, does it useExecute()
? – MaukLoadFunctionLibrary
but not sure – AvariaLoadFunctionLibrary()
procedure doesn't useExecuteGlobal
to execute the external vbs meaning it isn't available to global scope soGetRef()
will never see it. – MaukEval
only.LoadFunctionLibrary
is QTP's inbuilt function to import external code ... probably something fishy in there. Thanks. – AvariaExecuteFile
instead ofLoadFunctionLibrary
and that did the trick.LoadFunctionLibrary
is a recent development in QTP so assuming that it is not fully tested (which is ironic given QTP is a testing tool) – Avaria