Skip to content Skip to sidebar Skip to footer

Excel Vba: Get Content From Online Html Table

can anybody pleas show me part of VBA code, which will get text 'hello' from this example online HTML table? first node will be found by his ID (id='something'). ... exactly what you want, it will show you how to create something you can work with.

You'll need to use a mixture of getElementById() and getElemenetsByTagName() to retrieve your desired "hello"

eg: Document.getElementById("something").getElementsByTagName("tr")(1).getElementsByTagName("td")(2).innerText

  • Get the element "something"
  • Inside "something" get all "tr" tags (specifically the one at index 1)
  • Inside the returned tr tag get all "td" tags (specifically the one at index 2)
  • Get the innerText of the previous result

These objects use a 0 based array so the first item is item(0).

Update

document.getElementById() will return an (singular) IHTMLElement (which will include all of its children) or nothing/null if it does not exist.

document.getElementsByTagName() will return a collection of IHTMLElement (again, each element will include all of its children). (or an empty collection if none exist)

document.getElementsByTagName("tr") this will return all tr elements inside the "document" element.

document.getElementsByTagName("tr")(0) will return the first (singular) IHTMLElement from the collection. (note the index at the end?)

There is no (that i could find) "sibling" feature of the InternetExplorer object in VBA, so you'd have to do it manually using the child index.

Using the DOM Functions is the clean way to do it. Its much clearer than just looking at a chain "Element.Children(0).children(1).children(2)" as you've no idea what the index means without manually looking it up.

Solution 2:

I looked all over for the answer to this question, too. I finally found the solution by talking to a coworker which was actually through recording a macro.

I know, you all think you are above this, but it is actually the best way. See the full post here: http://automatic-office.com/?p=344 In short, you want to record the macro and go to data --> from web and navigate to your website and select the table you want.

I have used the above solutions "get element by id" type stuff in the past, and it is great for a few elements, but if you want a whole table, and you aren't super experienced, just record a macro. don't tell your friends and then reformat it to look like your own work so no one knows you used the macro tool ;)

Post a Comment for "Excel Vba: Get Content From Online Html Table"

As A Row

I need your help, if the following code below counts every …