проверять адреса с помощью макро-доступа USPS DOM Explorer - PullRequest
0 голосов
/ 28 августа 2018

Я использую приведенный ниже код для проверки адреса в Excel на веб-сайте USPS. Как я могу получить возвращенные данные из раздела DOM Explorer? Пожалуйста, смотрите прикрепленное изображение.

enter image description here

Я могу видеть данные из USPS, возвращаемые в IE, но я не могу получить данные в коде. Спасибо за вашу помощь

Мой код

Sub useClassnames()
    Dim element As IHTMLElement
    Dim elements As IHTMLElementCollection
    Dim ie As InternetExplorer
    Dim html As HTMLDocument

    Sheets("Address").Select
    erow = Sheet3.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

    'open Internet Explorer in memory, and go to website

    Set ie = New InternetExplorer
    ie.Visible = True

    ' Verify  addresses
    For r = 2 To 4
        myaddress = Cells(r, 1).Value
        mycity = Cells(r, 3).Value
        mystate = Cells(r, 4).Value
        myzipcode = Cells(r, 5).Value
        'myaddress = Range("a2").Value
        'mycity = Range("c2").Value
        'mystate = Range("d2").Value
        'myzipcode = Range("e2").Value

        'ie.navigate "https://tools.usps.com/go/ZipLookupAction!input.action"
        ie.navigate "https://tools.usps.com/zip-code-lookup.htm?byaddress"

        'Wait until IE has loaded the web page
        Do While ie.readyState <> READYSTATE_COMPLETE
            Application.StatusBar = "Loading Web page …"
            DoEvents
        Loop

        Set html = ie.document

        Set what = html.getElementsByName("tAddress")
        what.Item(0).Value = myaddress
        Set zipcode = html.getElementsByName("tCity")
        zipcode.Item(0).Value = mycity
        Set zipcode1 = html.getElementsByName("tState")
        zipcode1.Item(0).Value = mystate

        'Click the search button
        html.getElementById("zip-by-address").Click

        Do While ie.readyState <> READYSTATE_COMPLETE
            Application.StatusBar = "Loading Web page …"
            DoEvents
        Loop

        Set html = ie.document

        Set elements = html.getElementsByClassName("zipcode-result-address")

        For Each element In elements
            If element Like "*results*" Then
                MsgBox element
            End If
        Next element
        'End
    Next r
Set objie = Nothing
Set ele = Nothing
Set ie = Nothing
End Sub

1 Ответ

0 голосов
/ 29 августа 2018

Я согласен с Comintern, так как было бы намного чище использовать их API, но для того, чтобы показать вам, насколько вы были близки, см. Ниже.

Я полагаю, что следующий код достигнет того, что вы ожидаете, MessageBox с полным адресом, включая почтовый индекс:

Sub useClassnames()
Dim element As IHTMLElement
Dim elements As IHTMLElementCollection
Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim ws As Worksheet: Set ws = Sheets("Address")
'declare and set the worksheet you are working with, amend as required

'Sheets("Address").Select
erow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

'open Internet Explorer in memory, and go to website

Set ie = New InternetExplorer
ie.Visible = True

' Verify  addresses
    For r = 2 To 4
        myaddress = ws.Cells(r, 1).Value
        mycity = ws.Cells(r, 3).Value
        mystate = ws.Cells(r, 4).Value
        myzipcode = ws.Cells(r, 5).Value
        ie.navigate "https://tools.usps.com/zip-code-lookup.htm?byaddress"

        'Wait until IE has loaded the web page

        Do While ie.readyState <> READYSTATE_COMPLETE
            Application.StatusBar = "Loading Web page …"
            DoEvents
        Loop

        Set html = ie.document

        Set what = html.getElementsByName("tAddress")
        what.Item(0).Value = myaddress
        Set zipcode = html.getElementsByName("tCity")
        zipcode.Item(0).Value = mycity
        Set zipcode1 = html.getElementsByName("tState")
        zipcode1.Item(0).Value = mystate

        'Click the search button
        html.getElementById("zip-by-address").Click

        Do While ie.readyState <> READYSTATE_COMPLETE
            Application.StatusBar = "Loading Web page …"
            DoEvents
        Loop

        Set html = ie.document

        Set elements = html.getElementsByClassName("zipcode-result-address")

        For Each element In elements
            MsgBox element.innerText
        Next element
    Next r
Set objie = Nothing
Set ele = Nothing
Set ie = Nothing
End Sub
...