getElementById only if it exists

Using some vb code to extract some information out of a website
Occasionally the website goes down, and presents a 504 error page.

The element I'm trying to extract is not present on the 504 page.
How can I edit my code so it only extracts the information if it actually exists?

Thanks in advance

Set TDelements = HTMLdoc.getElementById("menu_content").getElementsByTagName("TD")

Open in new window

antoniokingAsked:
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.

rwniceingCommented:
Probably you want it on VBA on Excel, Right ? if so, test following code
to check object ID first before any info extraction

set  obj= HTMLdoc.getElementById("menu_content")

If obj Is Nothing Then
debug.print "obj not exist"
Else
debug.print "obj exist"
End If

Open in new window

0
MlandaTCommented:
Set MenuElement = HTMLdoc.getElementById("menu_content")

If MenuElement Is Nothing Then
        Set TDelements = Nothing
Else
        Set TDelements = MenuElement.getElementsByTagName("TD")
End If

'Make sure to check TDElements is not nothing before you use it
If Not TDElements Is Nothing Then

       'Do your thing here.

End If

Open in new window

However, I think the best is for you to check for the 504 when you load HTMLdoc. That way, you don't even go as far as trying to parse the HTML soon as you realise that the site is down. It is however still good practice to do lots of error checking and try to avoid making assumptions that (for example) the menu_element will always be there.
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
antoniokingAuthor Commented:
Thank you both for your help
0
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
VB Script

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.