Issue in VBA "getElementById"

Dim Doc As HTMLDocument
Set Doc = IE.Document

Dim sDD As Variant
sDD = Trim(Doc.getElementsByTagName("h1")(0).innerText)
Dim Topic As MSHTML.IHTMLElement
Set Topic = Doc.getElementById("description")

Open in new window


it return "[object HTMLMediaElement]" instead of the value from the webpage
karthik80cAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omgangIT ManagerCommented:
Dim Topic As MSHTML.IHTMLElement
Set Topic = Doc.getElementById("description")

You've set the variable Topic to an object and that's what is being displayed in the MsgBox (later in the procedure).  You need to change this
MsgBox topic
to this
MsgBox topic.innerHTML
or this
MsgBox topic.innerText

OM Gang
0
karthik80cAuthor Commented:
I have used "MsgBox topic.innerHTML" and "MsgBox topic.innerText", but it both returns null. So I tried to get the value with the tag name using the below code
 
Dim Topics As MSHTML.IHTMLElement
Dim Topic
Dim toppiicc
Topics = Trim(Doc.getElementsByTagName("h3")(0).innerText)
For Each Topic In Topics
toppiicc = Topic
Next

Open in new window

But it returns an error,
Run-time error '424':
Object required
0
omgangIT ManagerCommented:
From your original Q ( http://rdsrc.us/TuclLN ) I opened the URL specified and searched the source for the ID tag in question.

<div id="description">
      <h3>9%* NET RETURN FULLY LEASED RETAIL INVESTMENT</h3>
      <p class="body">- 723m2* well established Retail Shopping Centre<br/>- Good leases in place with long term tenants<br/>- 7 Tenancies including Foodworks/Pharmacy/Medical Centre/Real Estate<br/>- 3931m2* site with 34 on site car parks<br/>- Freestanding licensed restaurant<br/>- Kooralbyn Resort 200* metres away<br/>(*approximate.)<br/><br/>Owners are retiring & my instructions are clear - must be SOLD!<br/><br/>All approvals and details available in Information Memorandum on request.</p>
</div>

Your code is doing what it is supposed to; it's returning null because there is no content immediately within the specified element.  There are two additional html elements however and it appears you are wanting to get the content of one, or both, of those.  To do this take a look at the nextSibling property.

Dim Doc As HTMLDocument
Set Doc = IE.Document

Dim sDD As Variant
sDD = Trim(Doc.getElementsByTagName("h1")(0).innerText)
Dim Topic As MSHTML.IHTMLElement
Set Topic = Doc.getElementById("description").nextSibling   '<--- will get the <h3> element contained

Open in new window


OM Gang
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.