Excel VBA getElementsByTagName - 运行时错误 - 424

Excel VBA getElementsByTagName - Runtime error - 424

我正在使用以下参考资料:Visual Basic for Applications、Microsoft Excel 16.0 Object Library、OLE Automation、Microsoft Office 16.0 Object Library、Microsoft Internet Controls。

我挑出了下面我 运行 遇到此错误的那一行。我试过使用 innerText 和 textContent 但没有成功。

我也尝试了以下方法:http://automatetheweb.net/vba-getelementsbytagname-method/ 没有任何成功,因为我 运行 进入了 运行 时间错误 70。

我已经尝试 getElementsByClassName.getElementsByTagName 但没有成功。

我试过在 Microsoft Community answers 中发布这个问题,但我过去使用该网站一般收效甚微。

Sub ZipCodeRetrieve()

Dim ZipCodeRange As Range
Dim PopDensity As IHTMLElementCollection
Dim PopChange As IHTMLElementCollection
Dim IE As Object
Dim HTMLdoc As HTMLDocument

'Creates ie instance
With ThisWorkbook.Sheets("ZipCodes")
Set IE = New InternetExplorer
IE.Navigate "http://mcdc.missouri.edu/websas/caps10c.html"
IE.Visible = True

Set ZipCodeRange = Range("A2", .Range("A2").End(xlDown))
Debug.Print ZipCodeRange.Address

For Each cell In ZipCodeRange

    'allows ie to load before proceeding
    While IE.busy
        DoEvents
    Wend

    'looks for search box in Missouri.edu and inputs zipcode
    IE.document.all("latitude").Value = cell.Value
    'radius is constant
    IE.document.all("radii").Value = 75
    'clicks enter
    IE.document.forms(0).submit

    'allows ie to load before proceeding
    Do While IE.busy
        DoEvents
    Loop

    'preps ie for data collection
    Set HTMLdoc = IE.document

这是我遇到的错误

 Set PopDensity = HTMLdoc.getElementsByTagName("b").Item(18).innerText


    Set PopChange = HTMLdoc.getElementsByTagName("b").Item(10).textContent

    Debug.Print PopDensity.Value
    Debug.Print PopChange.Value

Next cell

End With

End Sub
Set PopDensity = HTMLdoc.getElementsByTagName("b").Item(18).innerText

您已将 PopDensity 声明为 IHTMLElementCollection,但此处您要从 innerText 中分配一个值,正如方法名称所暗示的那样,它是 return 文本,而不是collection.

试试这个:

Dim PopDensity 'As String 'or leave as variant
PopDensity = HTMLdoc.getElementsByTagName("b").Item(18).innerText