MyDaftQuestions
MyDaftQuestions

Reputation: 4691

How to get an object out of a collection in VBa

I used to have the following working code

Set result = DecodeJson(MyRequest.responseText)   
Dim keys() As String
keys = GetKeys(result.issues)

Now, my approach has changed and instead of having result being the object, I receive a Collection based upon Array of class objects as class member in VBA

Dim resultsFromQueries As Collection
Set resultsFromQueries = GetAllJSonObjects(searchString) ' this calls DecodeJson(MyRequest.responseText)  as per the code snippet above

Dim i As Integer
For i = 0 To resultsFromQueries.Count
    Dim keys() As String
    Dim item As Object
    Set item = resultsFromQueries.item(i + 1)  ' I guess it's not 0 based?
    keys = GetKeys(item.result.issues)   'KABOOM

The issue I have now is I keep getting the following exception

Run time error '424':
Object required

Checking in the watch window, item shows as type Variant/String and has the value of "[object Object]"

Do I need to cast it?

Upvotes: 1

Views: 1069

Answers (1)

Lucas Raphael Pianegonda
Lucas Raphael Pianegonda

Reputation: 1181

I had that problem just a few weeks ago, the problem was the following:

The collection was collected with the follwing code:Collection.Add(OBJECT) This however leads to the addition of the value of the Object into the Collection and can be solved as simple as leaving the parathesis.Collection.add OBJECT So my advice is to go into the GetAllJSonObjects(searchString) function and search for the addition process and make sure that it is without parethesis. Like this the Collection will contain Objects and not variants and your code with item(i+1) should work properly.

Good luck!

Upvotes: 1

Related Questions