PDA

View Full Version : Solved: How to read a table data cell from Internet Explorer thru VBA?



sappunni
06-12-2009, 06:35 AM
Hello,
I am trying to read the value of a table data cell from IE thru VBA.

The table data cell does have an ID, here is the sample HTML code

<tr>
<td width="20%" id="member_name_txt" class="tip" nowrap>Member Name</td>
<td width="20%">Jane Doe</td>
<td width="20%" id="member_id_txt" class="tip" nowrap>Member ID</td>
<td width="40%">123456789123</td>
</tr>
<tr>
<td id="address_txt" class="tip" nowrap>Address</td>
<td colspan="3">1234 AnyRoad DR </td>
</tr>
<tr>
<td></td>
<td colspan="3">AnyTown,&nbsp;IN&nbsp;&nbsp;12345-0000</td>
</tr>

For example, I want to read the words Jane Doe for the TD id member_name_txt.

I tried something like this:

webpage = ie.document.body.innertext
Webpage_Range.Offset(i - 1, j) = webpage
startpos = InStr(endpos, webpage, "member_name_txt") + 25
endpos = InStr(startpos, webpage, vbNewLine)
Length = endpos - startpos
webname = Mid(webpage, startpos, Length)

Is there a better way of accessing the table data cell using the ID ?



Thanks and appreciate any help !!!

Norie
06-12-2009, 09:55 AM
The id only refers to the first TD in the table.

The TD for Jane Doe has no ID.

Is this all the HTML?

Where are the TABLE tags?

You could try something like this to get all the data.


Sub test()
Dim IE As Object
Dim doc As Object
Dim colTR As Object
Dim colTD As Object
Dim tr As Object
Dim td As Object
Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True

'goto site
IE.Navigate "C:\VBAE HTML Test.html"

'wait for page to load

Do Until IE.ReadyState = 4: DoEvents: Loop

Set doc = IE.Document

Set colTR = doc.GetElementsByTagName("TR")

For Each tr In colTR

Set colTD = tr.GetElementsByTagName("TD")

For Each td In colTD
MsgBox td.innertext
Next td

Next tr

IE.Quit

End Sub

sappunni
06-24-2009, 10:53 AM
Thank you!!!