Solved

Looping through HTML objects using MSHTML.HTMLDocument

Posted on 2003-10-28
9
2,950 Views
Last Modified: 2007-12-19
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
Comment
Question by:jessy_houle
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 9641106
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9641406
listening...
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 9642173
http://www.freevbcode.com/ShowCode.Asp?ID=2442

My sample should show you how to recurse through the DOM using MSHTML
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jessy_houle
ID: 9647352
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
 
LVL 28

Accepted Solution

by:
AzraSound earned 500 total points
ID: 9650014
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9650280
Excelent work Azra, that little thing (Dim col As IHTMLElementCollection) is pretty nice.
;)
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 9650352
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9650457
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
 

Author Comment

by:jessy_houle
ID: 9651960
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question