3

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").

...
<table id="something">
  <tr>
    <td><TABLE><TR><TD></TD></TR><TR><TD></TD></TR></TABLE></td><td></td>
  </tr>
  <tr>
    <td></td><td></td><td>hello</td>
  </tr>
...

i think it will be something like child->sibling->child->sibling->sibling->child, but I don't know the exact way.

EDIT updated code tags are CAPITALS. so if I use getElemenetsById("something").getElemenetsByTagName('tr') it get only two tr tags to collection, or four (with tags which are deeper children)?

5
  • 1
    why minus 1 ? I think it's simple and clear question for anybody who work with this. Commented Apr 22, 2013 at 8:19
  • Im certain this exact question has been asked before, pretty sure I commented or gave an answer too.. Just cant find it Commented Apr 22, 2013 at 9:32
  • 1
    @NickSlash firstly. ok, if you gave an answer, you can see it in your profile, I think. secondly, before I asked, I tried to find answer by check links generated by input where you writing question. nothing relevant has been found. so I dont undestand, why minus 1, is it so hard to write answer, if you know it? Commented Apr 22, 2013 at 11:00
  • 1
    I didn't -1 your question. Commented Apr 22, 2013 at 11:08
  • 2
    @NickSlash At first sight it seemed. I apologize Commented Apr 23, 2013 at 6:54

2 Answers 2

5

If you did search for an answer, you might want to broaden your scope next time. There are plenty of questions and answers that deal with DOM stuff and VBA.

Use getElementById on HTMLElement instead of HTMLDocument

While the question (and answers) aren't 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.

Sign up to request clarification or add additional context in comments.

5 Comments

ok, thanks. I will try, but I think there must be straighter way, how to get elements in DOM tree. something like in other stackowerflow question that you send (Element.Children(1).Children(0).innerText). And in this example I can't see something like "getElementsByTagName("td")(2)", so if I find it, I still would not know answer.
The answer to the question used something like that and the IHTMLElement object does support the children property. Go read the documentation on the MSDN. A for a straighter way, I'm not sure what you mean.there it's no magic short hand way to do it.
so I tried. problem is, that table cell <td> can include whole new table. so if I use getElementById("something").getElementsByTagName("tr")(2) it collect all TRs under "something" element (right?). so than I will get first row from table contained in first row of base table. thats why I'm asking about clean way like children->sibling...
I wrote it little bit puzzled, sorry. this sentence it collect all TRs under "something" element (right?) should sounds like it collect all TRs under "something" element including these in the nested table and chose third of them (right?). I updated example in my question, so now it describes the situation better. maybe :-) The nested table is why I'm looking for sibling feature (if its working like in my updated question)
Yes it will get all the tr elements from your nested tables. Again, I don't believe the VBA implementation includes Sibling and next child etc. You'll have to loop over the result of your getElementsByTagName and make sure its parent node is the top level table.
1

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 ;)

2 Comments

thanks for comment. I have been looking for some more programming way like child/sibling. but it's good to know other solution
It is actually highly efficient too, so I don't think you are going to come up with anything better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.