TPJ87
TPJ87

Reputation: 61

Pull a specific <TD>'s innertext from webpage/html table

I'm trying to pull the innertext to the <TD> tag bracketing M118. I was trying to alter this :

Range("J" & (ActiveCell.Row)) = IE.Document.getelementbyid("ctl05_BasicdatagridInventoryLocations").getelementbyclass("gridItem").innerText

to find the table and row is there something I can add before innertext to select the <TD> I'm trying to pull from?

The HTML I'm working with is:

<table class="grid" cellspacing="0" cellpadding="1" rules="all" border="1" id="ctl05_BasicdatagridInventoryLocations" style="border-style:Solid;width:100%;border-collapse:collapse;">

<tr class="gridPager" align="left">
<td colspan="7"><span>1</span></td>
</tr>

<tr class="gridHeader">
<td>Quantity</td><td>Class</td><td>UOM</td><td>Warehouse</td><td>Location</td><td>Last Cycle Count</td><td>&nbsp;</td>
</tr>

<tr class="gridItem" onmouseover="cc(this, true);" onmouseout="cc(this, false);">
<td>5</td><td>NEW</td><td>EA</td><td>Rig Warehouse</td><td>M118</td><td>11 Sep 2013</td><td></td>
</tr>

</table>

Upvotes: 2

Views: 6179

Answers (1)

jacouh
jacouh

Reputation: 8741

You can use this (Tested in IE10):

Sub sof20167953GetIeWebpage()
  Dim objIe As Object
  Dim xobj

  Set objIe = CreateObject("InternetExplorer.Application")
  objIe.Visible = True
  '
  objIe.navigate "http://www.example.com/MyTable.html"
  '
  While (objIe.Busy Or objIe.READYSTATE <> 4)
    DoEvents
  Wend
  '
  Set xobj = objIe.Document.getElementById("ctl05_BasicdatagridInventoryLocations").getElementsByClassName("gridItem").Item(0)
  Set xobj = xobj.getElementsByTagName("td").Item(4)
  '
  Range("J" & (ActiveCell.Row)) = xobj.innerText
  '
  Set xobj = Nothing
  '
  objIe.Quit
  Set objIe = Nothing

End Sub

Please notice:

  1. getElementById() gets a single element.
  2. getElementsByClassName() gets a collection of elements, so use .Item(0) to get first element
  3. getElementsByTagName() gets a collection of elements, so use .Item(0) to get first element

Upvotes: 1

Related Questions