Solved

Get table data with WebBrowser

Posted on 2004-08-02
5
554 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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!

Question has a verified solution.

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

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month5 days, 17 hours left to enroll

627 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