Looping through HTML objects using MSHTML.HTMLDocument

How do I loop through TD tags in multiple, unnamed, table(s).  For example, I have HTML in an MSHTML.HTMLDocument object, which contains TABLEs with the words POINTS, SCORE, and TOTAL (TH tags), also contains 3 TRs with 3 TDs that contain data like (10, 6, 54), (12, 6, 34), (2, 5, 23) with the following structure:

...
<TABLE>
<TR><TH>POINTS</TH><TH>SCORE</TH><TH>TOTAL</TH></TR>
<TR><TD>10</TD><TD>6</TD><TD>54</TD></TR>
<TR><TD>12</TD><TD>6</TD><TD>34</TD></TR>
<TR><TD>2</TD><TD>5</TD><TD>23</TD></TR>
</TABLE>
...
<TABLE>
<TR><TH>POINTS</TH><TH>SCORE</TH><TH>TOTAL</TH></TR>
<TR><TD>3</TD><TD>3</TD><TD>23</TD></TR>
<TR><TD>3</TD><TD>3</TD><TD>45</TD></TR>
<TR><TD>6</TD><TD>2</TD><TD>32</TD></TR>
</TABLE>
...

I want to look for all tables that contain the words POINTS, SCORE, and TOTAL, and loop through the data inside the TDs, to eventually store in a database with the following DDL:

CREATE TABLE stats (
  team_id int(11) NOT NULL,
  points int(11) NOT NULL,
  score int(11) NOT NULL,
  total int(11) NOT NULL
)

However, I'm only looking for the looping through the MSHTML collection(s) part.  Here is the start:

Dim obIE As SHDocVw.InternetExplorer
Dim obIEDoc As MSHTML.HTMLDocument
...
Set obIEDoc = New MSHTML.HTMLDocument
Set obIEDoc = obIE.Document

{for each blah in blah}

Thank you.
jessy_houleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzzzzoocCommented:
The below example loops using indexes within the element collection. You could use "For Each" in the collection enumeration, but my method of doing what you want requires accessing the 2nd & 3rd items in the collection.


Requirements:

1.) Add a reference to "Microsoft HTML Object Library"

2.) Create "c:\test.html" with the following:

<html><body>

<TABLE>
<TR><TH>POINTS</TH><TH>SCORE</TH><TH>TOTAL</TH></TR>
<TR><TD>10</TD><TD>6</TD><TD>54</TD></TR>
<TR><TD>12</TD><TD>6</TD><TD>34</TD></TR>
<TR><TD>2</TD><TD>5</TD><TD>23</TD></TR>
</TABLE>
...
<TABLE>
<TR><TH>POINTS</TH><TH>SCORE</TH><TH>TOTAL</TH></TR>
<TR><TD>3</TD><TD>3</TD><TD>23</TD></TR>
<TR><TD>3</TD><TD>3</TD><TD>45</TD></TR>
<TR><TD>6</TD><TD>2</TD><TD>32</TD></TR>
</TABLE>

</body></html>



-----------------------



Private Sub Form_Load()
    Dim doc1 As New HTMLDocument, doc2 As New HTMLDocument
    Dim col1 As IHTMLElementCollection, iLoop As Integer
    Dim sPoints As String, sScore As String, sTotal As String
    Set doc1 = doc2.createDocumentFromUrl("file://c:\test.html", "null")
    Do Until doc1.readyState = "complete"
        DoEvents
    Loop
    Set col1 = doc1.All.tags("TD")
    For iLoop = 0 To col1.length Step 3
        If iLoop + 2 < col1.length Then
            sPoints = col1.Item(iLoop).outerText
            sScore = col1.Item(iLoop + 1).outerText
            sTotal = col1.Item(iLoop + 2).outerText
            MsgBox sPoints & "," & sScore & "," & sTotal
        End If
    Next iLoop
End Sub
0
Richie_SimonettiIT OperationsCommented:
listening...
0
AzraSoundCommented:
http://www.freevbcode.com/ShowCode.Asp?ID=2442

My sample should show you how to recurse through the DOM using MSHTML
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jessy_houleAuthor Commented:
First and foremost, thank you all for responding.  However, this isn't exacly what I'm looking for.

I need to loop through the TABLEs collection in the DOCUMENT object.  After I find the TABLE containing the word "POINTS", I want to loop through that TABLE's TD collection.

Something more like:

    For Each {tableobject} In {document}
        If InStr(1, {tableobject}.innerHTML, "POINTS") Then
            For Each {tdobject} In {tableobject}.cells
              Debug.Print {tdobject}.innerHTML & vbCrLf
            Next
        End If
    Next

Please just give me the nexted loop above.

Thank you.
0
AzraSoundCommented:
Dim col As IHTMLElementCollection

'not sure about the class names, but they should look something like these
Dim tbl As HTMLTableElement        
Dim tr As HTMLTableRow
Dim td As HTMLTableCell

Set col = obIE.Document.All.tags("TABLE")
For Each tbl In col
    If Instr(1, tbl.innerText, "POINTS") <> 0 Then
        For Each tr In tbl.rows
            For Each td In tr.cells
                Debug.Print td.innerText
            Next
        Next
    End if
Next
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richie_SimonettiIT OperationsCommented:
Excelent work Azra, that little thing (Dim col As IHTMLElementCollection) is pretty nice.
;)
0
AzraSoundCommented:
I usually don't reference the MSHTML Object Library so I couldn't remember the actual class name...I usually just declare it as Object.  However, I noticed zzzzzooc's sample above used it so that made it easier on my memory.   ;-)
0
Richie_SimonettiIT OperationsCommented:
i think the only problem is with this:
Dim tbl As HTMLTableElement
it should be
Dim tbl As HTMLTable

Your memory is working OK.
0
jessy_houleAuthor Commented:
Excellent work AzraSound!  Thank you.  And, Richie_Simonetti, you are correct, it should be HTMLTableElement.  Thank you all very much!  This is a huge asset for myself, as well as other's who search for this solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.