Excel VBA getElementsByTagName - 运行时错误 - 424

时间:2017-10-03 17:47:20

标签: excel vba excel-vba getelementsbytagname

我正在使用以下参考:Visual Basic for Applications,Microsoft Excel 16.0对象库,OLE自动化,Microsoft Office 16.0对象库,Microsoft Internet Controls。

我已经单独列出了我遇到此错误的行。我已经尝试过使用innerText和textContent而没有任何成功。

我还尝试了以下这个方法:http://automatetheweb.net/vba-getelementsbytagname-method/但没有成功,因为我遇到了运行时错误70.

我试过getElementsByClassName.getElementsByTagName但没有成功。

我尝试在Microsoft社区的答案中发布此问题,但过去使用该网站的成功率很低。

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

1 个答案:

答案 0 :(得分:1)

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

您已将PopDensity声明为IHTMLElementCollection,但此处您正在为innerText指定一个值,因为方法名称建议使用返回文字,而不是集合。

试试这个:

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