getElementById only if it exists

Posted on 2014-08-30
Last Modified: 2014-09-04
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

Question by:antonioking
    LVL 1

    Assisted Solution

    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"
    debug.print "obj exist"
    End If

    Open in new window

    LVL 30

    Accepted Solution

    Set MenuElement = HTMLdoc.getElementById("menu_content")
    If MenuElement Is Nothing Then
            Set TDelements = Nothing
            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.

    Author Closing Comment

    Thank you both for your help

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now