Solved

Get table data with WebBrowser

Posted on 2004-08-02
5
534 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

773 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