代码之家  ›  专栏  ›  技术社区  ›  Nahuatl_C137

Excel VBA getElementsByTagName-运行时错误-424

  •  0
  • Nahuatl_C137  · 技术社区  · 9 年前

    我正在使用以下参考资料:Visual Basic for Applications、Microsoft Excel 16.0对象库、OLE Automation、Microsoft Office 16.0对象库、Microsoft Internet控件。

    http://automatetheweb.net/vba-getelementsbytagname-method/ 没有任何成功,因为我遇到了运行时错误70。

    我试图获取ElementsByclassName。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
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Tim Williams    9 年前
    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