Home » excel » VBA Excel – IE GetElementByID not working – Runtime error 424 Object Required

VBA Excel – IE GetElementByID not working – Runtime error 424 Object Required

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m having trouble getting Excel VBA (Office 2016) to control IE (version 11). I need to enter information into several fields in a form and click on a few radio buttons. I’ve identified the object IDs for all the fields I need to modify. My code is copied/modified from mature code that I wrote a few years ago to do similar things on another web site. Though it continues to work on the other website today, it isn’t working here. Unfortunately, it is a proprietary website, so I can’t share the link, but here is some of my code and a portion of the web page code:

Sample of my code:
Dim IE As Object, objShell As Object, objCollection As Object

' Take control of the webpage:

' Get the page url
URL = "---------"

' Find the correct instance of IE
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For X = 0 To (IE_count - 1)
    On Error Resume Next
    my_title = objShell.Windows(X).Document.Title
    my_url = objShell.Windows(X).LocationURL
    If IsEmpty(my_url) Then
        my_url = objShell.Windows(X).Document.Location
    End If

    If my_url Like Left(URL, 45) & "*" Then 'compare to find if the desired web page is already open
        Set IE = objShell.Windows(X)
        marker = 1
        Exit For
    Else
    End If
Next
If marker = 0 Then
    MsgBox ("Webpage not found. Please login manually.")
    Exit Sub
End If
On Error GoTo -1
On Error GoTo 0
' Wait if necessary
Do Until (IE.ReadyState = 4 And Not IE.Busy)
    DoEvents
    Application.StatusBar = "Waiting for Internet Explorer."
Loop
Application.StatusBar = False

' Set objCollection object
Set objCollection = IE.Document.getElementByID("QSHAA5V0GH4LSAO2AI5R2M853SJ5AI") 
' This is the actual ID of one of the fields I need to enter data into (see screenshot below)
' The above line gives the familiar "Runtime error 424 Object Required" error.

Screenshot of HTML code for webpage

Screenshot of HTML code for webpage

Why would this work for one website and not another?

As an additional note: when I use

Set objCollection = IE.Document.getElementsByClassName("Class")
MsgBox objCollection.Length

it sometimes results in one or more objects being collected, but usually results in zero. Once I have the object I need in the collector I’m good, but I’m not there yet.

If you see a mistake I’m making, I would be very grateful to learn about it, as I’m very confused right now.

I have the following libraries included:

Visual Basic For Applications

Microsoft Excel 16.0 Object Library

OLE Automation

Microsoft Office 16.0 Object Library

Microsoft Forms 2.0 Object Library

Microsoft ActiveX Data Objects 6.1 Library

Microsoft Internet Controls

Microsoft HTML Object Library

***** Here is a (modified) screenshot of the webpage I am working with if that helps at all. *****

Webpage screenshot

Looking at the screenshot, should I be looking for an object within an object? Do I need to find the parent object (the protruding form within the web page) before I can find the child object (field on the form)?

How to&Answers:

I found the answer! I should have posted just a little bit more of the web browser code in the picture above and someone else would have found it hours ago. Here it is with the extra code:

Larger screenshot of HTML code from webpage

The top-left corner of the screenshot shows the tagname “iframe” (which is new to me today). The objects I need are contained within this iframe object. Once I found this out, I found this discussion which helped me create this line of code:

Set objCollection = IE.Document.getElementsByTagName("iframe")(1).contentDocument.getElementsByTagName("input")

(The value of (1) is because I have two iframe objects and I am using the second one.)

Once I have this, it is just a matter of looking for the object with the correct ID in the collection!

I could dance for joy!