MSHTML Version and getElementsByClassName

Doug alerted me that Rob van Gelder’s In-Cell Charting post was yet another casualty of the great data loss of 2011. I got the post back up, but there were 85 comments on that post that really deserve to be put back. And I’m working on that.

To that end, I wrote some code that includes the getElementsByClassName method of the HTMLLIElement object.

I’m parsing through the HTML from the wayback machine to reconstruct the database entries. When I ran this code on a different machine, I got an error. It did not recognize getElementsByClassName as a valid method. I checked the Object Browser and, sure enough, it wasn’t listed. Googling was fruitless. The only thing I saw was that IE doesn’t support getElementsByClassName prior to version 9. Well, that doesn’t apply to me. I’m not automating IE, I’m using the MSHTML Object Library.

So I check the version of mshtml.tlb and find that it’s 4.0.

I check on the other machine and…

The exact same version with different members. Nice. Finally, it dawned on me that I probably have never updated IE on the second system. I checked and noted that I still had version 8 installed. I updated to IE9 and the code ran perfectly. I checked the ObjectBrowser and the method was listed. I checked the TLB version and it’s still 4.0.

Clearly I don’t understand how object libraries work. I had assumed that MSHTML was independent of IE. It makes sense that they’re related, but I would have thought that IE9 used the object definitions in MSHTML, not the other way around. In reality, they’re probably both using object definitions from a third file. Anyway, I’m happy to have the problem solved, if not the mystery.

2 thoughts on “MSHTML Version and getElementsByClassName

  1. It can behave oddly, for example, I’ve never been able to make getElementsByTagName to work with late binding, since it only appears to work when the type is explicitly defined. For example:

    Works, whereas:


  2. Hi, the same with HTMLfile object in Windows Scripting. Try to run this VB script:

    set xmlhttp = createobject(“Microsoft.XMLHttp”) “GET”, “”, false
    resp = xmlhttp.responsetext
    set doc = createobject(“htmlfile”)
    doc.write resp
    set tags = doc.getelementsbytagname(“*”)
    msgbox tags.length
    set tags = doc.getelementsbyclassname(“ajaxContent”)
    msgbox tags.length

    Result first msgbox shows total tags – ok, and doc.getelementsbyclassname gives above exception. I run at Windows 7 HB, SP 1, x64;
    IE 9.0.8112.16421, Update 9.0.12 (KB2761465).

Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.