• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3201
  • Last Modified:

vba code with getelementbyid works in ie8 but not ie9

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 "http://www.subdomain.mydomain.com/path/thispage.aspx"
        Do While .Busy
        Sleep 100
        Sleep 1000

failsHere:  x.Document.getElementById("txt471id").Value = n471
 End With
 End Sub
  • 2
  • 2
1 Solution
The element doesn't have an ID, only a name.

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

Open in new window

rberkeAuthor Commented:
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
This worked for me:
x.Document.getElementsBytagName("input")(2).Value = n471

Open in new window

rberkeAuthor Commented:
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.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now