访问 vba IE getElementById "object required" 错误 424

access vba IE getElementById "object required" error 424

这与这个问题有关

此代码直到几天前才有效,此后唯一的变化是更改为 Windows 10,但这应该不会影响它,对吗?

另外,好像是抓取了第一条记录,然后就报错了。但是,如果我单击 DEBUG,然后 STEP OUT,它会工作并转到下一条记录,我必须再次单击 DEBUG、STEP OUT 等等。 81次。

从中调用了其他函数,它们只是抓取更多内容并将其放入 table,我不认为它们是问题所在,但如果需要我可以添加它们。

给出错误424的行是

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

这是代码

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

问题是因为运行代码太快

所以要么在行前加一个睡眠

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

或者开始检查是否有return值

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
...