Excel VBA getElementsByTagName – 运行时错误 – 424

我正在使用下列引用:Visual Basic for Applications,Microsoft Excel 16.0对象库,OLE自动化,Microsoft Office 16.0对象库,Microsoft Internet控件。

我已经单独列出了我遇到这个错误的地方。 我试过使用innerText和textContent没有任何成功。

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

我试图getElementsByClassName.getElementsByTagName没有任何成功。

我已经尝试在Microsoft社区的答案张贴这个问题,但我一直在使用该网站在过去一般很less成功。

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 

你已经声明PopDensityIHTMLElementCollection但是在这里你是从innerText分配一个值,它的方法名称暗示的是返回文本,而不是集合。

尝试这个:

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