QTP: Object Required errors when Object is valid

tsilb picture tsilb · Feb 17, 2009 · Viewed 7.5k times · Source

I have the following VBScript in a reusable action:

'Gather links
Browser("1").Navigate "http://InternalWebmail/something/inbox.nsf"
set oDesc = Description.Create()
oDesc("micclass").Value = "Link"
set links = Browser("1").Page("Webmail").ChildObjects(oDesc)
Dim links2
links2 = ""

'Filter out irrelevant links
For i = 0 To links.Count-1
    If lcase(trim(links(i).GetROProperty("text"))) = lcase(trim(DataTable("ExpectedFrom", dtGlobalSheet))) Then
    links2 = links2 + "," + links(i).GetROProperty("url")
    End If
Next

Dim final
final = split(mid(links2,2),",")  'Remove leading comma and split into array

'For each link (i.e. for each E-mail received):
'Effectively giving a reusable action an input parameter, I hope

For i = 0 To final.Count - 1  'error: Object Required
    DataTable("url","CheckHeader") = final(i)
    RunAction "CheckHeader", oneIteration
Next

Everything runs just fine, until I get to the declaration of the loop at the bottom of the snippet. There, QTP gives me an error "Object Required" and refuses to elaborate.

  • i has a leading value of 58, though I've tried setting it to 0 prior to entering the loop.
  • final is an array of 6 strings, each a URL. All have a value.
  • If I msgbox(final(2)), I see the value of final(2) as being valid.
  • isobject(final(1)) = false
  • final(1) has the correct value
  • msgbox(final is nothing) and msgbox(final(1) is nothing) yield the same error.

It looks as if the array is null but somehow the array has members? How is this possible? What is QTP talking about?

Answer

Motti picture Motti · Feb 17, 2009

In vbscript arrays don't have a Count property, you should use UBound

x = split("how now brown cow")
' MsgBox x.Count ' error
MsgBox UBound(x) ' 3

The reason .Count worked for the first loop is that ChildObjects does not return an array, it returns a COM collection object. That is also why you had to use the Set statement when assigning to links but not when assigning to final.