Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA - Find preceding html tag

Tags:

html

excel

vba

Say I have HTML source that looks like this

<div id="book-info"> 
  <span class="title">Weather</span>
  <span class="title">Title Of Book</span>
  <p><a href="http://test.com?MMC_ID=34343">Buy Now</a></p>
</div>

What I need returned is "Title Of Book"

There are numerous instances of span class="title" but the one I need immediately precedes the only MMC_ID tag on the page, so I can use MMC_ID as a marker to get close to the span tag I need.

Question: How can I say "Grab the contents of the very first span tag to the left of MMC_ID?

The below code works sometimes, but there is a variable number of span tags on the page so it fails when that deviation occurs.

With CreateObject("msxml2.xmlhttp")
    .Open "GET", ActiveCell.Offset(0, -1).Value, False
    .Send
    htm.body.innerhtml = .ResponseText
End With

ExtractedText = htm.getElementById("book-info").getElementsByTagName("span")(1).innerText
like image 527
LoxBagel Avatar asked Jan 03 '14 20:01

LoxBagel


1 Answers

This should do it

Text_1 = htm.getElementById("book-info").innerhtml
if instr(1, text_1, "MMC_ID ", vbTextCompare) > 0 then
   numb_spans = htm.getElementById("book-info").getElementsByTagName("span").length
   ExtractedText = htm.getElementById("book-info").getElementsByTagName("span")(-1 + numb_spans).innerText
else
end if
like image 120
ron Avatar answered Oct 05 '22 00:10

ron