How to remove result getElementsBy *

I am doing an internet search from VBA, and at some point I have a string

Set valueResult = currPage.getElementById("rg_s").getElementsByTagName("IMG")

where is currPagedeclared asHTMLDocument

and I wonder how Dim valueResultto achieve:

  • IntelliSense Results (vba autocomplete)
  • Better lead time (using a specific type, not the default type Variant)

When I look at the Locals window, they tell me what Setgives it   DispHTMLElementCollection, but this is not an option when I am Dim. I read that it getElementsBy*actually returns Node Listas opposed to an array, so I try to follow this prospectus, but cannot find anything specific to VBA.

So how can I declare it? - At the moment, I just got it Dim valueResult As Object, but this is hardly better than Variantit does not give IntelliSense invitations.

NB. I have a Microsoft HTML Object Library marked

+4
source share
2 answers

He must be IHTMLElementCollection.

Dim valueResult As IHTMLElementCollection

'// your code here

Set valueResult = currPage.getElementById("rg_s").getElementsByTagName("IMG")

The method getElementsByTagName()returns Collectionbecause there are potentially multiple matches. The method getElementById()returns an element, thereforeIHTMLElement


quick example:

Sub test()

Dim col As IHTMLElementCollection
Dim item As IHTMLElement

Set IE = CreateObject("InternetExplorer.Application")

IE.Navigate "http://www.google.co.uk"

While IE.ReadyState <> 4
    DoEvents
Wend

Set col = IE.Document.getElementsByTagName("a")

For Each i In col
    Set item = i
    Debug.Print item.outerText
Next

IE.Quit

End Sub
+2
source
  • The most important thing is to add links:
    • Microsoft Internet Controls (SHDocVw)
    • Microsoft HTML Object Library

, intellisense, . Object intellisense , Visual Basic , . .

enter image description here

:

Set IE = CreateObject("InternetExplorer.Application")

:

Dim IE As SHDocVw.InternetExplorer
Set IE = New SHDocVw.InternetExplorer 

Microsoft HTML Object Library , Dim col As IHTMLElementCollection, .

Object Browser :

enter image description here

enter image description here

+1

Source: https://habr.com/ru/post/1673395/


All Articles