Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3751
  • Last Modified:

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.
0
jessy_houle
Asked:
jessy_houle
  • 3
  • 3
  • 2
  • +1
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now