Solved

Get table data with WebBrowser

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

830 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