peter.dignard
peter.dignard

Reputation: 11

Add Specific HTMLElement's to a HTMLElementCollection

I am not a very experienced coder, and I am trying to put to create a web-scraping tool that does not need to be very powerful/eloquent. My issue is that the only way I can scrape data off a specific website is by using each SHTMLelement's id. I then want to put all of those element's into one element collection, but I can't figure out how to do this. Here is my code:

    Dim IE As New SHDocVw.InternetExplorerMedium
    
    Dim HTMLDoc As MSHTML.HTMLDocument
    Dim HTMLgrades As MSHTML.IHTMLElementCollection ' The collection I want to make
    Dim HTMLgrade1 As MSHTML.IHTMLElement
    Dim HTMLgrade2 As MSHTML.IHTMLElement
    Dim HTMLgrade3 As MSHTML.IHTMLElement  'there are 100's of grades, but I will just use three here
    
    IE.Visible = True
    IE.navigate "website I am navigating to"
   
    ' waiting mechanism for website to load

    Set HTMLDoc = IE.Document
    Set HTMLgrade1 = HTMLDoc.getElementById("longidname_1")
    Set HTMLgrade2 = HTMLDoc.getElementById("longidname_2")
    Set HTMLgrade3 = HTMLDoc.getElementById("longidname_3")

I have tried all types of code to add each element to the elementcollection, but I keep getting errors. I know there is most likely a super simple solution, so I appreciate any help I can get!

Upvotes: 0

Views: 295

Answers (1)

Tim Williams
Tim Williams

Reputation: 166885

For example:

Dim IE As New SHDocVw.InternetExplorerMedium

Dim HTMLDoc As MSHTML.HTMLDocument
Dim col As New Collection, arr, id
 
IE.Visible = True
IE.navigate "website I am navigating to"

' waiting mechanism for website to load

arr = Array("longidname_1", "longidname_2", "longidname_3")
For Each id In arr
    col.Add HTMLDoc.getElementById(id)
Next

Upvotes: 0

Related Questions