Jendra found an interesting bug while automating Internet Explorer. The Document object has a dozen or so collections that allow you to access various components of the html document. For instance, you can use the Links collection to iterate through all the hyperlinks (anchor tags with an href attribute) or you can use the All collection to iterate HTMLElements of any type.
Jendra has an image tag with a name attribute of “links”. It seems when you have an element with a name attribute equal to the name of a collection. I’ve tested it with Links and All and I assume it screws up on any collection name.
I don’t see a decent workaround to this bug. Jendra decided to test the Typename of the objects in the loop and exit gracefully if it was different than expected. Of course that means the code won’t actually work, but at least it won’t throw an ugly run-time error.
Here’s the test code. When the Navigate line is changed to TestLinks2, no error. As it is, I get error 438 “Object doesn’t support this property or method”. In debug mode from the immediate window you can see why
Of course an HTMLImg object doesn’t have a Length property so the For i = 1 to… line fails.
Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim i As Long
Set ieApp = New InternetExplorer
Loop Until ieApp.ReadyState = READYSTATE_COMPLETE
Set ieDoc = ieApp.Document
Debug.Print “******Links collections”
For i = 1 To ieDoc.links.Length
Debug.Print i, Mid(ieDoc.links(i – 1).innertext, 1, 20), TypeName(ieDoc.links(i – 1))
Debug.Print “******All collection”
For i = 1 To ieDoc.all.Length
Debug.Print i, Mid(ieDoc.all(i – 1).innertext, 1, 20), TypeName(ieDoc.all(i – 1))