Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get table data with WebBrowser

Posted on 2004-08-02
5
Medium Priority
?
571 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 1600 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses

824 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