Solved

Get table data with WebBrowser

Posted on 2004-08-02
5
530 Views
Last Modified: 2010-08-05
I'm interfacing with a webpage through the webbrowser control and I need to get the data out of the cells.  I use something like this to go through all of the links in a page with the webbrowser control:

Dim doc As HTMLDocument
Dim a_link As HTMLAnchorElement
Dim txt As String

On Error Resume Next
Set doc = WebBrowser1.Document
For Each a_link In doc.links
    msgbox a_link.href
Next a_link

Is there a way for me to go through a table similar to this?  

I'm trying to pull the data from a table that has 3 columns and a dynamic number of rows and need to pull the data from each column and every row.  Thanks for your help.
0
Comment
Question by:cheatc1
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 400 total points
ID: 11693600
Try the below. Should be thorough enough. :}


Form1:
--------------
Option Explicit
Private Sub Form_Load()
    Call WebBrowser1.Navigate("file://c:\test.html")
End Sub
Private Sub Command1_Click()
    Const strTable As String = "table"
    Dim objDoc As HTMLDocument
    Dim objCol As IHTMLElementCollection
    Dim objTbl As HTMLTable
    Dim objTblRow As HTMLTableRow
    Dim objTblCol As HTMLTableCol
    Set objDoc = WebBrowser1.Document
    Set objCol = objDoc.All.tags(strTable)
    If objCol.length > 0 Then
        'HTMLTable - .Item(0) = first table...
        Set objTbl = objCol.Item(0)
        Set objCol = objTbl.children
        If objCol.length > 0 Then
            'HTMLTableSelection.. child of HTMLTable and parent of
            'HTMLTableRow
            Set objTbl = objCol.Item(0)
            'Loop through rows...
            For Each objTblRow In objTbl.children
                'Loop through cols...
                For Each objTblCol In objTblRow.children
                    'MsgBox a single column...
                    Call MsgBox(objTblCol.innerText)
                Next objTblCol
                'MsgBox the entire row...
                'Call MsgBox(objTblRow.innerText)
            Next objTblRow
        End If
    End If
End Sub
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 11693624
Below is the testing HTML I used. .Children() should work fine but it *may* require type-checks if somehow you have non row/col child elements of HTMLTableSelection.

file://c:\test.html:
--------------------------
<html>
<body>

<table>
<tr bgColor="#999999">
<td>1,1</td><td>1,2</td><td>1,3</td>
</tr>
<tr bgColor="#999999">
<td>2,1</td><td>2,2</td><td>2,3</td>
</tr>
<tr bgColor="#999999">
<td>3,1</td><td>3,2</td><td>3,3</td>
</tr>
</table>

</body>
</html>
0
 

Author Comment

by:cheatc1
ID: 11694483
Wow, great.  Could you help me out on one more thing though.  Say I have two tables.  On one, the td classes are named "table1" and the second table, the td classes are named "table2" like <td class="table1">

I only want results from the second table where the td class="table2".  Do you know how to do this.  Thanks for your help.
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 11694580
Change the index to direct to your table..

        'HTMLTable - .Item(0) = first table...
        Set objTbl = objCol.Item(0)

to possibly..

        Set objTbl = objCol.Item(1)
0
 

Author Comment

by:cheatc1
ID: 11694651
Perfect coding, thanks for your time.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

758 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

12 Experts available now in Live!

Get 1:1 Help Now