vba code with getelementbyid works in ie8 but not ie9

Posted on 2012-09-17
Last Modified: 2012-09-18
The following code simply fills a text box. It works in IE8 but fails in IE9. It gives error 424 "object required".  Both versions running under windows 7 pro.

Please help.

Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub t507()
Dim x As Object
Set x = CreateObject("InternetExplorer.Application")
With x
Dim n471 As Long
n471 = 123456
        .Visible = True
        .Navigate ""
        Do While .Busy
        Sleep 100
        Sleep 1000

failsHere:  x.Document.getElementById("txt471id").Value = n471
 End With
 End Sub
Question by:rberke
    LVL 5

    Expert Comment

    The element doesn't have an ID, only a name.

    Have you tried:
    x.Document.getElementByName("txt471id").Value = n471

    Open in new window

    LVL 5

    Author Comment

    The method getElementByName gives error 438 "object does not support this method or property."

    I have tried the following variations .
    getElementById  (gives error 424)
    getElementByName  error 438
    getElementsByClassName error 438
    getElementsByName error 438
    getElementsByTagName error 438
    getElementsByTagNameNS error 438
    LVL 5

    Accepted Solution

    This worked for me:
    x.Document.getElementsBytagName("input")(2).Value = n471

    Open in new window

    LVL 5

    Author Comment

    Thanks, that got me going.   The better syntax is

    With x.Document.getElementsByName("txt471id")

    x.Document.getElementsByName("txt471id")(0).value = n471

    But, even that is not perfect.  Unlike getElementById, there is no guaranteed that getElementsByName will will be unique.  So the following code is better.  

        If .Length <> 1 Then
            MsgBox "Error--there are " & .Length & " items with name txt471id"
            x.Document.getElementsByName("txt471id")(0).value = n471
        End If
    End With

     IE8 probably noticed that that txt471id was unique and and treated it like an Id.  My guess is that IE9 is doing a better job of conforming to industry standards.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
    When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure ( It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
    The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
    Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

    759 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

    8 Experts available now in Live!

    Get 1:1 Help Now