+ Reply to Thread
Results 1 to 24 of 24

Testing if an object exists... explanation required

  1. #1
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Testing if an object exists... explanation required

    When I run this code and the object does not exist it works...

    Please Login or Register  to view this content.
    The issue I have is that without the "ON ERROR" statement I get a runtime 424 error when the object does not exist. (at the set line)
    Using "On Error Resume Next" brings with it problems of its own.




    I have also tested this code, which works BUT it is noticeably slower than using the 'On error' code (above).

    Please Login or Register  to view this content.

    My question(s):
    How does one overcome the dilemma that you can't set an object that does not exist but you can't test if an object exists until you set it?
    Or can you?
    If so how?

  2. #2
    Registered User
    Join Date
    12-08-2012
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2016
    Posts
    10

    Re: Testing if an object exists... explanation required

    On Error Resume Next doesn't resolve the error that presents when the object doesn't exist. Check out Chip Pearson's blog on the subject <http://bit.ly/29MKyGd>.

    You might want to replace On Error Resume Next with On Error Goto ResolveMissingObject (or some such tag). And of course include the tag and code to handle the error.

    And could the reason the If Then code takes so long is that the document contains no element that matches the eleID in the doc.
    Last edited by Kevin_N_PA; 12-26-2017 at 04:38 PM. Reason: To append my comments.

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Testing if an object exists... explanation required

    In OOPs, a reference variable-dDETAIL- either points to an object or not which is "NULL"- it does not point to an object.
    In VBA, you have "Nothing" instead of "NULL".

    Set dDETAIL = doc.getElementById(eleID)
    I do not know the main reason for code not returning nothing if there is no object, but I suspect I do not think the set statement is meant to be clear-cut as either returning object or nothing. In VBA most things work or do not work through trial and error.
    This line my prove my point.

    If doc.getElementById(eleID) > 0 Then rng = doc.getElementById(eleID).outerText

  4. #4
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Testing if an object exists... explanation required

    Thanks for the reply Kevin.

    Chip Pearson's explanation on error handling is a good learning tool. However, my question is not really about error handling.

    I am querying the logic behind
    Please Login or Register  to view this content.
    and generally testing if an object exists.
    It is my understanding that you can NEVER have an object that is nothing simply because you can't set an object that is nothing.
    Therefore the
    Please Login or Register  to view this content.
    code is redundant/pointless.
    In order to test if an object is something you must first set it, if the object is nothing then it can't be set.

    So does that mean the only way to test if an object is nothing is to set it and trap the error if indeed it is nothing?
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Testing if an object exists... explanation required

    Quote Originally Posted by AB33 View Post
    In OOPs, a reference variable-dDETAIL- either points to an object or not which is "NULL"- it does not point to an object.
    In VBA, you have "Nothing" instead of "NULL".

    Set dDETAIL = doc.getElementById(eleID)
    I do not know the main reason for code not returning nothing if there is no object, but I suspect I do not think the set statement is meant to be clear-cut as either returning object or nothing. In VBA most things work or do not work through trial and error.
    This line my prove my point.

    If doc.getElementById(eleID) > 0 Then rng = doc.getElementById(eleID).outerText

    Thanks for the reply AB33.
    My understanding is:
    Please Login or Register  to view this content.
    returns a count of the element that you're retrieving, doesn't it?
    Given that it is searching for an element by ID , the count can only be 1 (if it exists) or zero (ie it does not exist). If you search by tagname then I assume you can get a return value of more than 1?

    I included that bit of code because from what I understand it does actually test if an element exists. I think that it runs slower because it does the same thing twice, looks for the element to see if it exists , then looks for it again in order to get the outertext.

    On my simplified example it would appear that it is more efficient using error trapping to test if an object exists?
    Would that be a reasonable assumption to make?
    Last edited by anrichards22; 12-26-2017 at 06:46 PM.

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Testing if an object exists... explanation required

    Maybe you want:

    If Len(doc.getElementById(eleID)) > 0 Then rng = doc.getElementById(eleID).outerText
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Testing if an object exists... explanation required

    Is it because get element by ID might be a single value, while get element by Name returns an array look like-List nodes? An array is an object while a single value might not be in VBA.

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Testing if an object exists... explanation required

    Quote Originally Posted by anrichards22 View Post
    It is my understanding that you can NEVER have an object that is nothing simply because you can't set an object that is nothing.
    I'm afraid your understanding is not correct. An object variable is either an object or it is Nothing. Often, when using an assignment, you do have to use an error handler and then test for Is Nothing.

    It is not clear what you meant by this statement-
    Using "On Error Resume Next" brings with it problems of its own
    Would you care to elaborate?
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  9. #9
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Testing if an object exists... explanation required

    xlnitwit,

    That is the bit of the logic that makes no sense to me.

    If an object variable is either an object or nothing then:
    Please Login or Register  to view this content.
    In theory x is either:
    1. SomeObject or
    2. Nothing

    However, if x is NOTHING then VBA throws an error when you try to set it rather than setting it as nothing.
    Is there an explanation for this?

    In relation to On Error Resume Next:
    I was merely suggesting that skipping over errors (on error resume next) leads to unpredictable results.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Testing if an object exists... explanation required

    I don't understand your example. This works fine:

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Testing if an object exists... explanation required

    Quote Originally Posted by anrichards22 View Post
    However, if x is NOTHING then VBA throws an error when you try to set it rather than setting it as nothing.
    Is there an explanation for this?
    VBA does not throw an error when you try and set an object to Nothing. There are some methods that throw an error rather than simply returning Nothing when they fail, but that is not the same thing. Consider the Find method, which will happily return Nothing if the search value is not found.

    In relation to On Error Resume Next:
    I was merely suggesting that skipping over errors (on error resume next) leads to unpredictable results.
    Indeed. That is why one should limit the scope of an On Error Resume Next statement to as small as possible- preferably just one line- before resetting error handling, via an On Error Goto 0 or other method.

  12. #12
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Testing if an object exists... explanation required

    VBA does not throw an error when you try and set an object to Nothing. There are some methods that throw an error rather than simply returning Nothing when they fail, but that is not the same thing. Consider the Find method, which will happily return Nothing if the search value is not found.
    I understand that now, however, the question still remains, why? Why doesn't it just return nothing rather than failing?
    I am sure there is a good explanation, I am interested in learning what that reason is.

  13. #13
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Testing if an object exists... explanation required

    What do you mean by "it" exactly? Some methods return Nothing, others will raise an error. It just depends on how they were programmed.

  14. #14
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Testing if an object exists... explanation required

    "it" being the object variable that I am trying to set.

    Please Login or Register  to view this content.
    As in my code above

    Please Login or Register  to view this content.
    If there is no element with that eleID then instead of dDetail =Nothing it raises an error.
    Which made the next line of my code redundant/pointless...
    Please Login or Register  to view this content.
    ...it is pointless because if dDetail is nothing then the previous line will raise an error.

  15. #15
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Testing if an object exists... explanation required

    It is this part that raises an error
    Please Login or Register  to view this content.
    not the variable or the Set statement. It is simply the way that the getElementbyID method was coded- presumably for internal consistency. Therefore you require the error handler
    Please Login or Register  to view this content.
    note the red line to clear the effect of the OERN line.

  16. #16
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Testing if an object exists... explanation required

    Did you previously set doc?

  17. #17
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Testing if an object exists... explanation required

    Yes, I did previously set doc. The code loops through a series of ID's, sometimes they are all there, sometimes one is missing.

    It is this part that raises an error
    doc.getelementbyID(eleID)
    I don't think it is that part that raises the error.

    Please Login or Register  to view this content.
    ...raises an error when eleID does NOT exist

    Please Login or Register  to view this content.
    ...Does NOT raise an error when eleID does NOT exist.

  18. #18
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Testing if an object exists... explanation required

    Quote Originally Posted by shg View Post
    I don't understand your example. This works fine:

    Please Login or Register  to view this content.
    As I understand it now...different methods treat objects differently..

    I am just curious as to why.

    Specifically in relation to html objects...I have searched far and wide and almost always the advice given to test if a html element exists is
    Please Login or Register  to view this content.

  19. #19
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Testing if an object exists... explanation required

    What is the error message when eleId doesn't exist?

  20. #20
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Testing if an object exists... explanation required

    Error is 424

  21. #21
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Testing if an object exists... explanation required

    That is the error for this line
    Please Login or Register  to view this content.
    ?

  22. #22
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Testing if an object exists... explanation required

    Yes.
    When the eleID does not exist.

  23. #23
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Testing if an object exists... explanation required

    That seems very strange indeed. I would only expect that error if the doc variable were Nothing.

  24. #24
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Testing if an object exists... explanation required

    Quote Originally Posted by anrichards22 View Post
    As I understand it now...different methods treat objects differently..

    I am just curious as to why.

    Specifically in relation to html objects...I have searched far and wide and almost always the advice given to test if a html element exists is
    Please Login or Register  to view this content.
    It seems there is a third option: An error. In VBA find function, you get two options: either points to a object or nothing, but this consistency does not work in DOM objects. IMO, it is a waste of time to find out the reason for inconsistency.
    Last edited by AB33; 12-28-2017 at 06:18 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] explanation required for HD serial no same on 3 different pc
    By JEAN1972 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-26-2017, 10:06 AM
  2. VBA explanation required
    By ntljennifer in forum Excel General
    Replies: 2
    Last Post: 06-27-2014, 09:05 PM
  3. Multi Level Dependent Drop-down list, Explanation required
    By Shoieb.arshad in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-31-2012, 11:36 AM
  4. Replies: 3
    Last Post: 10-25-2011, 02:12 PM
  5. Testing if a network file exists?
    By GasSensor in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-27-2008, 07:32 AM
  6. [SOLVED] A better explanation of help required
    By Elvey in forum Excel General
    Replies: 3
    Last Post: 05-20-2006, 12:28 PM
  7. Simple explanation of formula required pls
    By Swn-Y-Mor in forum Excel General
    Replies: 4
    Last Post: 01-14-2005, 08:48 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1