简体   繁体   中英

VBA returns [object] when using Doc.getElementsByTagName. Can't locate correct TagName from browser

I'm having trouble locating the correct tag name from Chrome. When I run the following code all that is returned is "[object]". Does anyone have any suggestions to pull in the correct field?

Here's the website I'm attempting to pull information. Specifically Census Tract but at this point if someone could show me how to return any information I will mine the correct one.

Here's my code, currently running the output to a MsgBox just to find the right field. From there I will reenable to for statement and make the URL in the VBA dynamic. Also note that I've attempted to use getElementsByName but no progress there either:

Sub censusTract()
Dim sht As Worksheet
Dim lastRow As Long

Set sht = ActiveWorkbook.Sheets("Sheet1")
lastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

'For i = 2 To lastRow
 Dim IE As New InternetExplorer
 'IE.Visible = True
 IE.navigate "http://geocoding.geo.census.gov/geocoder/geographies/address?street=1308+Lapwing+Rd&city=Edmond&state=OK&zip=73003&benchmark=4&vintage=4"
 Do
 DoEvents
 Loop Until IE.readyState = READYSTATE_COMPLETE
 Dim Doc As HTMLDocument
 Set Doc = IE.document
 Dim sDD As String
 'sDD = Doc.getElementsByTagName("br")(0)
 sDD = Doc.getElementsByName("pl_gov_census_geo_geocoder_domain_AddressResult")(0)
 MsgBox sDD
 'IE.Quit
 'sht.Cells(i, 41).Value = sDD
'Next i
End Sub

You are very close. All you need to do is pull the correct data from the object. You can use one of the following

sDD = Doc.getElementsByName("pl_gov_census_geo_geocoder_domain_AddressResult").item(0).innerText
sDD = Doc.getElementsByName("pl_gov_census_geo_geocoder_domain_AddressResult").item(0).innerHTML
sDD = Doc.getElementsByName("pl_gov_census_geo_geocoder_domain_AddressResult").item(0).outerHTML

Right now its just pulling the object, adding innerText, innerHTML or outerHTML should pull your result.

Once you have that pulled you may want to split the results into an array so you can do what you need with each of the elements. the code below should get you started.

Sub censusTract()
Dim sht As Worksheet
Dim lastRow As Long, v As Variant, block As Variant, x As Integer


Set sht = ActiveWorkbook.Sheets("Sheet1")
lastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

'For i = 2 To lastRow
 Dim IE As New InternetExplorer
 'IE.Visible = True
 IE.navigate "http://geocoding.geo.census.gov/geocoder/geographies/address?street=1308+Lapwing+Rd&city=Edmond&state=OK&zip=73003&benchmark=4&vintage=4"
 Do
 DoEvents
 Loop Until IE.readyState = READYSTATE_COMPLETE
 Dim Doc As HTMLDocument
 Set Doc = IE.document
 Dim sDD As String
 'sDD = Doc.getElementsByTagName("br")(0)
 sDD = Doc.getElementsByName("pl_gov_census_geo_geocoder_domain_AddressResult").Item(0).innerText
 block = Split(sDD, vbLf)
 x = 2 'start column
 For Each v In block
  If v <> "" Then
    Cells(i, x).Value = v
    x = x + 1
  End If
Next v
 'IE.Quit
 'sht.Cells(i, 41).Value = sDD
'Next i
End Sub

Let me know if you have any questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM