JasonG
JasonG

Reputation: 19

Run-time error 438 Object doesn't support this property or method

I'm dealing with a problem that's been dealt this before, but not in this situation.

I'm pulling addresses from the USPS website using VBA. When I place in my cell "ele.innertext" I get all of the innertext within the class, but VBA won't let me isolate the innertext to the item level - ele.item(1).innertext, for example, give me the above error. Do you know why?

My browser is IE11.

Relevant HTML:

<div id="zipByAddressDiv" class="industry-detail">Loading...</div>

                            <!-- start Handlebars template -->
                            <script id="zipByAddressTemplate" type="text/x-handlebars-template">
                                <ul class="list-group industry-detail">
                                {{#each addressList}}
                                    <li class="list-group-item paginate">
                                        <div class="zipcode-result-address">
                                            <p>{{companyName}}</p>
                                            <p>{{addressLine1}}</p>
                                            <p>{{city}} {{state}} <strong>{{zip5}}-{{zip4}}</strong></p>

VBA:

   Sub USPS()

Dim eRow As Long
Dim ele As Object
Dim objie As Object
Dim wscript As Object
Dim test As String
Dim testarray() As String
'Dim goods As Object
Dim r As Integer
Dim x As Long: x = 0
Dim vFacility As Variant
Dim y As Variant
'Dim IE As New InternetExplorer
Sheets("Address").Select

eRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Set objie = CreateObject("InternetExplorer.Application")

For r = 4 To 8

myaddress = Cells(r, 5).Value
mycity = Cells(r, 7).Value
mystate = Cells(r, 8).Value
myzipcode = Cells(r, 9).Value

'myaddress = Range("a2").Value
'mycity = Range("c2").Value
'mystate = Range("d2").Value
'myzipcode = Range("e2").Value


With objie
.Visible = True
.navigate "https://tools.usps.com/go/ZipLookupAction!input.action"

Do While .Busy
DoEvents
Loop



Set what = .document.getElementsByName("tAddress")
what.Item(0).Value = myaddress
Set zipcode = .document.getElementsByName("tCity")
zipcode.Item(0).Value = mycity
Set zipcode1 = .document.getElementsByName("tState")
zipcode1.Item(0).Value = mystate
Set zipcode2 = .document.getElementsByName("tZip-byaddress")
zipcode2.Item(0).Value = myzipcode

.document.getElementByID("zip-by-address").Click


Do While .Busy
DoEvents
Loop


 For Each ele In .document.all

Select Case ele.className
Case "industry-detail"
test = ele.innertext
testarray = Split(test, vbCrLf)

Worksheets("Address").Cells(r, 11).Value = testarray(4)

'Debug.Print test
'Debug.Print "and"
'Debug.Print testarray(4)

End Select

Next ele
End With



Next r
Set objie = Nothing
Set ele = Nothing
Set IE = Nothing

'IE.Quit


End Sub

Upvotes: 0

Views: 910

Answers (1)

QHarr
QHarr

Reputation: 84465

What I think you are trying to do is input address details and retrieve the found zipcode. This method uses CSS selectors to target the page styling and I start immediately with the address search URL. I use id selectors where possible (which is the same as saying .document.getElementById("yourID"), denoted by # as these are the quickest retrieval methods. When it comes to choosing state, which is a dropdown, I select the appropriate option. You could concantenate the search state 2 letter abbreviation into the option string e.g.

Dim state As String 
state = "NY"
.querySelector("option[value=" & state &  "]").Selected = True

There is a loop to ensure the target element is present in new search results page. I use another CSS selector of #zipByAddressDiv strong to target just the zipcode, which is in bold, in the results. The bold is set by the strong tag.

strong tag holding zipcode in result:

enter image description here

CSS query:

enter image description here

The above CSS selector is target by id using #zipByAddressDiv and then, rather than splitting into an array to get the value you want, it uses a descendant selector to target the strong tag element holding the required value.


VBA:

Option Explicit
Public Sub AddressSearch()
    Dim IE As New InternetExplorer, t As Date, ele As Object
    Const MAX_WAIT_SEC As Long = 5

    With IE
        .Visible = True
        .navigate "https://tools.usps.com/zip-code-lookup.htm?byaddress"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            .querySelector("#tAddress").Value = "1 Main Street"
            .querySelector("#tCity").Value = "New York"
            .querySelector("option[value=NY]").Selected = True
            '  .querySelector("#tZip-byaddress").Value = 10045
            .querySelector("#zip-by-address").Click
        End With

        While .Busy Or .readyState < 4: DoEvents: Wend

        t = Timer
        Do
            DoEvents
            On Error Resume Next
            Set ele = .document.querySelector("#zipByAddressDiv strong")
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While ele Is Nothing

        Debug.Print ele.innerText
        .Quit
    End With
End Sub

Here is what that looks like in a loop:

Option Explicit
Public Sub AddressSearch()
    Dim IE As New InternetExplorer, t As Date, ele As Object, i As Long
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Address")
    Const MAX_WAIT_SEC As Long = 5

    With IE
        .Visible = True

        For i = 4 To 8

            .navigate "https://tools.usps.com/zip-code-lookup.htm?byaddress"

            While .Busy Or .readyState < 4: DoEvents: Wend

            With .document
                .querySelector("#tAddress").Value = ws.Cells(i, 5).Value
                .querySelector("#tCity").Value = ws.Cells(i, 7).Value
                .querySelector("option[value=" & ws.Cells(i, 8).Value & "]").Selected = True
                '  .querySelector("#tZip-byaddress").Value = 10045
                .querySelector("#zip-by-address").Click
            End With

            While .Busy Or .readyState < 4: DoEvents: Wend

            t = Timer
            Do
                DoEvents
                On Error Resume Next
                Set ele = .document.querySelector("#zipByAddressDiv strong")
                On Error GoTo 0
                If Timer - t > MAX_WAIT_SEC Then Exit Do
            Loop While ele Is Nothing

            ws.Cells(i, 11) = ele.innerText
            Set ele = Nothing
        Next
        .Quit
    End With
End Sub

Upvotes: 1

Related Questions