Solved

Looping through HTML objects using MSHTML.HTMLDocument

Posted on 2003-10-28
9
2,581 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now