Search code examples
vbams-accessgetelementbyid

access vba IE getElementById "object required" error 424


this is related to this question

scrape data from a table on a website without having to search for tags

this code worked up until a few days ago, the only change since then was changing to Windows 10 but that shouldn't affect it, should it?

also, it seems that it scrapes the first record and then gives the error. however, if i click DEBUG, then STEP OUT, it works and goes to the next record and i again have to click DEBUG, STEP OUT and so on. 81 times.

There are other functions being called from it, they just scrape more stuff and put it in a table, i don't think they're the issue, but i can add them if needed.

The line that gives the error 424 is

If .Document.getElementById("middleContent_lbType").outerHTML Like "*General Acute Care Hospital*" Then

here's the code

Public Sub VisitPages()

DoCmd.RunSQL "DELETE FROM ScrapedFacs"

    AutoID = 1
    Dim ie As New InternetExplorer
    'Set ie = New InternetExplorerMedium
    With ie
        .Visible = False
        .navigate "http://healthapps.state.nj.us/facilities/acSetSearch.aspx?by=county"

        While .Busy Or .ReadyState < 4: DoEvents: Wend

        With .Document
            .querySelector("#middleContent_cbType_1").Click
            .querySelector("#middleContent_cbType_4").Click
            .querySelector("#middleContent_btnGetList").Click
        End With

        While .Busy Or .ReadyState < 4: DoEvents: Wend

        Dim list As Object, i  As Long
        Set list = .Document.querySelectorAll("#main_table [href*=doPostBack]")
        For i = 0 To list.Length - 1
            list.Item(i).Click

            While .Busy Or .ReadyState < 4: DoEvents: Wend

If .Document.getElementById("middleContent_lbType").outerHTML Like "*General Acute Care Hospital*" Then
FacType = "General Acute Care Hospital"
ElseIf .Document.getElementById("middleContent_lbType").outerHTML Like "*Psychiatric Hospital*" Then
FacType = "Psychiatric Hospital"
End If


Address = Replace(Replace(Replace(.Document.getElementById("middleContent_lbAddress").outerHTML, "<span id=" & Chr(34) & "middleContent_lbAddress" & Chr(34) & ">", ""), "<br>", ", "), "</span>", "")

            WriteTable .Document.getElementsByTagName("table")(3), .Document.getElementById("middleContent_lbName_county").innerText


            'do stuff with new page
            .Navigate2 .Document.URL             '<== back to homepage
            While .Busy Or .ReadyState < 4: DoEvents: Wend
            Set list = .Document.querySelectorAll("#main_table [href*=doPostBack]") 'reset list (often required in these scenarios)

        Next
       ' Stop                                     '<== Delete me later
        .Quit '<== Remember to quit application
    End With
End Sub

Solution

  • The issue is because of running code too fast

    So either add a sleep before the line

    Sleep 1
    ...Document.getElementById("middleContent_lbType")..
    

    Or start checking if there is a return value or not

    Set obj = .Document.getElementById("middleContent_lbType")
    If obj is Nothing:
       Sleep 1
       Set obj = .Document.getElementById("middleContent_lbType")
    End If
    
    If obj.outerHTML Like "*General Acute Care Hospital*" Then
    ...