+ Reply to Thread
Results 1 to 10 of 10

Why is this NOT producing a type mismatch error in VBA?

  1. #1
    Registered User
    Join Date
    07-10-2013
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    77

    Why is this NOT producing a type mismatch error in VBA?

    I'm declaring a variable as an HTMLAnchorElement object which is reserved for html "a" tags. Then I am setting the variable equal to a paragraph tag which should only work for HTMLParaElement objects. Somehow the variable becomes a paraelement object despite being declared otherwise.

    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    07-10-2013
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    77

    Re: Why is this NOT producing a type mismatch error in VBA?

    Does anyone at least agree that the expected result would be a type mismatch?

  3. #3
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Why is this NOT producing a type mismatch error in VBA?

    Probably because HTMLAnchorElement is just an interface, just as HTMLParaElement is likely to be. I can't remember what the base object is, probably something like HTMLElement, so it's not particularly surprising.

  4. #4
    Registered User
    Join Date
    07-10-2013
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    77

    Re: Why is this NOT producing a type mismatch error in VBA?

    Quote Originally Posted by Kyle123 View Post
    Probably because HTMLAnchorElement is just an interface, just as HTMLParaElement is likely to be. I can't remember what the base object is, probably something like HTMLElement, so it's not particularly surprising.
    I was under the impression these were actual objects. Would interfaces show up in the object browser?

    Also, let's say it is an interface....In order to avoid a type mismatch, the sub-interface would have to be encapsulated by the super interface, correct? So the HTMLParaElement would have to implement the HTMLanchorelement interface at the very least. But it works fine and the same thing happens if you switch the paragraph element with the anchor element.

    Also, in my further testing, even if I declare el as an htmlanchorelement but then try to set it to IE.document, it still works fine. How could that be given that htmlanchorelement would be below htmldocument on the interface hierarchy?

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Why is this NOT producing a type mismatch error in VBA?

    No, they all just have to implement the HTMLNodeElement (or whatever the base element is). What makes you think they're not interfaces? Yes, interfaces would still show up.

  6. #6
    Registered User
    Join Date
    07-10-2013
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    77

    Re: Why is this NOT producing a type mismatch error in VBA?

    Quote Originally Posted by Kyle123 View Post
    No, they all just have to implement the HTMLNodeElement (or whatever the base element is). What makes you think they're not interfaces? Yes, interfaces would still show up.
    I believe this is the base element you're referring to. I guess what I'm most confused about is that if these are ALL interfaces, what is the actual OBJECT? Is the object in the object browser?

    Second most confusing to me is why it would behave this way just because they both implement the same parent interface. If I declare variable "el" as an anchor element, and then want to do a "For Each el" loop for all elements in the page, it will actually loop through every element rather than only all the anchor elements just because they all implement the same parent element.

    Thanks for your help - you're the first one I've come across in a while that is experienced with interfaces!
    Last edited by dmasters4919; 07-13-2015 at 12:33 PM.

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Why is this NOT producing a type mismatch error in VBA?

    Dunno, haven't looked, the node is probably the actual object. Whatever it may be called in the object browser.

    HTML is hierarchical, so you're better off navigating using the dom. You can do something like:

    Please Login or Register  to view this content.
    If you want to loop through specific types, you should be able to Dim el as HTMLAnchorElement for the above.

    BTW I'm only guessing

  8. #8
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Why is this NOT producing a type mismatch error in VBA?

    When you declare an object variable As something, all you're telling the compiler is that it will be an object that implements the interface you specified. The Object itself could be a totally different class. (that's why you can declare any object variable as Object and then pass any class to it)
    Remember what the dormouse said
    Feed your head

  9. #9
    Registered User
    Join Date
    07-10-2013
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    77

    Re: Why is this NOT producing a type mismatch error in VBA?

    Quote Originally Posted by romperstomper View Post
    When you declare an object variable As something, all you're telling the compiler is that it will be an object that implements the interface you specified. The Object itself could be a totally different class. (that's why you can declare any object variable as Object and then pass any class to it)
    In this scenario, the core object would implement both the HTMLanchorelement interface, the HTMLparaelementinterface, as well as the HTMLdocument interface. The HTMLdocument object would also implement the individual element interfaces for some reason?

    Since I can declare el as an HTMLdocument but then set it equal to a paragraph element?

  10. #10
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Why is this NOT producing a type mismatch error in VBA?

    It would seem so. I couldn't tell you why!

+ 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] Error 13 type mismatch
    By Jiptastic in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-03-2013, 05:14 PM
  2. Run Type error 13 Type Mismatch
    By Affan Khan in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 11-13-2012, 12:58 PM
  3. [SOLVED] Run-type error 13 type mismatch
    By misop in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-05-2012, 04:08 AM
  4. [SOLVED] Type Mismatch Error in VBA
    By juliorevka in forum Excel General
    Replies: 4
    Last Post: 04-20-2012, 12:26 PM
  5. Complile Error: Type Mismatch ??? After adding error trap
    By clemsoncooz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-22-2012, 03:50 PM
  6. Conditional Formatting - Run Time Error '13' Type Mismatch Error
    By ksp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-17-2011, 07:37 PM
  7. [SOLVED] Help: Compile error: type mismatch: array or user defined type expected
    By lvcha.gouqizi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2005, 05:05 PM
  8. [SOLVED] VBA type mismatch error
    By Garbunkel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-03-2005, 11:05 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