Solved

Looping through HTML objects using MSHTML.HTMLDocument

Posted on 2003-10-28
9
2,690 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

896 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

17 Experts available now in Live!

Get 1:1 Help Now