none
Correct address through Internet Explorer automation and USPS.COM

    Dotaz

  • Hello everyone,
    I'm trying to correct an address usiing the United States Postal Sevice through COM automation with IE9. Below is the code streamlined:
    DECLARE INTEGER Sleep IN WIN32API INTEGER nTimeout
    
    oIE   = CREATEOBJECT("InternetExplorer.Application")
    oIE.Navigate("https://tools.usps.com/go/ZipLookupAction!input.action?mode=0&refresh=true")
    oIE.Visible = .T.
    
    DO WHILE oIE.Busy = .T.
    	DOEVENTS
    	LOOP
    ENDDO
    
    DO WHILE oIE.ReadyState <> 4
    	Sleep(100)
    ENDDO
    
    oIE.Document.All("tAddress").Value = "1320 NW 14 ST"
    oIE.Document.All("tApt").Value = ""
    oIE.Document.All("tCity").Value = "Miami"
    
    oIE.Document.All("sState").Value = "FL"
    oIE.Document.All("Zzip").Value = "33125"
    oIE.Document.Forms(0).Submit()
    
    OIE.Quit

    I have two problems:
    1 - The line:  oIE.Document.All("sState").Value = "FL" ; points to a dropdown and it doesn't seem to respond to the value assignment.
    2 - I can't have the form to submit the values populated in the fields. I tried looking the source of the form to find the elements name
    and use those names to no avail.
    oIE.Document.Forms(0).Submit()
    oie.DOCUMENT.FORMS.lookupZipFindBtn.Submit()
    Any suggestions?
    Thanks in advance,
    Luis
     
    • Přesunutý Mike Feng 24. listopadu 2011 13:56 VBS (From:Visual FoxPro General)
    21. listopadu 2011 21:32

Odpovědi

  • OLE Automation was not the solution for this; below is a draft of the code we ended up using

    LOCAL lcHttpCmd, lcStreet, lcCity, lcState, lcZip4
    LOCAL lnIDx, lcTmpString, lcChar, lcApt
    LOCAL lcURL, oHttp, lcMessage, lnStatus 
    
    lcURL		  = 'https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=0'
    
    lcStreet      = "1600 Pennsylvania Ave NW"
    lcCity        = "Washington"
    lcState   	  = "DC"
    
    lcZip		  = "20500"
    lcZip4		  = ""
    lcApt		  = ""
    
    lcMessage     = ""
    
    *Eliminate double spaces
    lcTmpString = lcStreet 
    DO WHILE AT(SPACE(2),lcTmpString) > 0
    	lcTmpString = STRTRAN(lcTmpString, SPACE(2), SPACE(1))
    ENDDO
    
    lcStreet = STRTRAN(lcTmpString, SPACE(1), "+")
    lcApt    = STRTRAN(lcApt, SPACE(1), SPACE(0))
    
    *Eliminate double spaces
    lcCity = ALLTRIM(lcCity)
    lcTmpString = lcCity
    
    DO WHILE AT(SPACE(2),lcTmpString) > 0
    	lcTmpString = STRTRAN(lcTmpString,SPACE(2),SPACE(1))
    ENDDO
    lcCity 	  = STRTRAN(lcTmpString, SPACE(1), "+")
    
    lcHttpCmd = "&companyName=&address1=" + lcStreet + "&address2=" + ALLTRIM(UPPER(lcApt)) + "&city=" + lcCity + "&state=" + lcState + "&urbanCode=&postalCode=&zip=" + lcZip
    
    oHTTP 	  = CREATEOBJECT("Microsoft.XMLHTTP")
    oHTTP.OPEN("POST", lcURL,.F.)
    oHTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    oHTTP.SEND(lcHttpCmd)
    
    lnStatus = oHTTP.STATUS
    
    CREATE CURSOR tmpHttp (htmlText M)
    APPEND BLANK
    REPLACE htmlText WITH oHTTP.responseText IN tmpHttp
    
    SELECT tmpHttp
    DIMENSION laLines[1]
    lnAmtLines = ALINES(laLines, tmpHttp.htmlText)
    
    llInvalidAddress = .F.
    
    FOR lnIDx = 1 TO lnAmtLines
    	lcString = ALLTRIM(laLines(lnIDx))
    
    	DO CASE
    		CASE  'NONDELIVERYMSG' $ UPPER(lcString)
    			llInvalidAddress = .T.
    			IF EMPTY(lcMessage)
    				lcMessage = "The address you provided is not recognized by the US Postal Service as an address we serve. Mail sent to this address may be returned."
    			ELSE
    				lcMessage = lcMessage  + CHR(13) + "The address you provided is not recognized by the US Postal Service as an address we serve. Mail sent to this address may be returned."
    			ENDIF
    			
    		CASE 'CLASS="ERROR"' $ UPPER(lcString)
    			IF EMPTY(THIS.edtMessage.VALUE)
    				lcMessage  = SUBSTR(lcString, AT('class="error"', lcString) + 14, AT('</li>', lcString) - AT('class="error"', lcString) -14)
    			ELSE
    				lcMessage  = THIS.edtMessage.VALUE + CHR(13) + SUBSTR(lcString, AT('class="error"', lcString) + 14, AT('</li>', lcString) - AT('class="error"', lcString) -14)
    			ENDIF
    			
    	ENDCASE
    
    	IF EMPTY(lcString)
    		LOOP
    	ENDIF
    
    	IF !'CITY RANGE' $ UPPER(lcString)
    		LOOP
    	ENDIF
    
    	lcCity 	= SUBSTR(lcString, AT('city range',lcString) + 12, AT('</span>',lcString) - AT('city range',lcString)-12)
    	lcState 	= SUBSTR(lcString, AT('state range',lcString) +13, AT('</span> <span class="zip"', lcString) - AT('state range', lcString) -13)
    
    	IF "HYPHEN" $ UPPER(lcString)
    		lcZip 	= SUBSTR(lcString, AT('zip',lcString) +14, AT('<span class="hyphen">',lcString) - AT('zip',lcString) - 21)
    		lcZip4 	 = SUBSTR(lcString, AT('zip4',lcString) +6,4)
    	ELSE
    		lcZip 	= SUBSTR(lcString, AT('zip',lcString) +14, AT('</span><span class="zip4"', lcString) - AT('zip', lcString) - 14)
    		lcZip4 	= ""
    	ENDIF
    
    
    	lcString = laLines[lnIDx - 3]
    	lcStreet  = SUBSTR(lcString, AT('address1 range', lcString) + 16, AT('</span>', lcString) - AT('address1 range', lcString) - 16)
    	
    	lcAddress = lcStreet + CHR(13) +;
    				lcCity + ", " + lcState + " " + lcZip + IIF(EMPTY(lcZip4), "", "-" + lcZip4)
    	
    	MESSAGEBOX(lcAddress, 64, "System Message")			
    
    	EXIT
    
    ENDFOR
    
    USE IN SELECT('tmpHttp')
    


    Luis

    • Označen jako odpověď LazyCod3r 13. února 2017 17:30
    13. února 2017 17:30

Všechny reakce

  • Hi Lazy,

    Do you try to check oIE.Document vaild?

    Have a nice day.


    Call me ghost for short, Thanks
    To get the better anwser, it should be a better question.
    23. listopadu 2011 18:32
  • OLE Automation was not the solution for this; below is a draft of the code we ended up using

    LOCAL lcHttpCmd, lcStreet, lcCity, lcState, lcZip4
    LOCAL lnIDx, lcTmpString, lcChar, lcApt
    LOCAL lcURL, oHttp, lcMessage, lnStatus 
    
    lcURL		  = 'https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=0'
    
    lcStreet      = "1600 Pennsylvania Ave NW"
    lcCity        = "Washington"
    lcState   	  = "DC"
    
    lcZip		  = "20500"
    lcZip4		  = ""
    lcApt		  = ""
    
    lcMessage     = ""
    
    *Eliminate double spaces
    lcTmpString = lcStreet 
    DO WHILE AT(SPACE(2),lcTmpString) > 0
    	lcTmpString = STRTRAN(lcTmpString, SPACE(2), SPACE(1))
    ENDDO
    
    lcStreet = STRTRAN(lcTmpString, SPACE(1), "+")
    lcApt    = STRTRAN(lcApt, SPACE(1), SPACE(0))
    
    *Eliminate double spaces
    lcCity = ALLTRIM(lcCity)
    lcTmpString = lcCity
    
    DO WHILE AT(SPACE(2),lcTmpString) > 0
    	lcTmpString = STRTRAN(lcTmpString,SPACE(2),SPACE(1))
    ENDDO
    lcCity 	  = STRTRAN(lcTmpString, SPACE(1), "+")
    
    lcHttpCmd = "&companyName=&address1=" + lcStreet + "&address2=" + ALLTRIM(UPPER(lcApt)) + "&city=" + lcCity + "&state=" + lcState + "&urbanCode=&postalCode=&zip=" + lcZip
    
    oHTTP 	  = CREATEOBJECT("Microsoft.XMLHTTP")
    oHTTP.OPEN("POST", lcURL,.F.)
    oHTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    oHTTP.SEND(lcHttpCmd)
    
    lnStatus = oHTTP.STATUS
    
    CREATE CURSOR tmpHttp (htmlText M)
    APPEND BLANK
    REPLACE htmlText WITH oHTTP.responseText IN tmpHttp
    
    SELECT tmpHttp
    DIMENSION laLines[1]
    lnAmtLines = ALINES(laLines, tmpHttp.htmlText)
    
    llInvalidAddress = .F.
    
    FOR lnIDx = 1 TO lnAmtLines
    	lcString = ALLTRIM(laLines(lnIDx))
    
    	DO CASE
    		CASE  'NONDELIVERYMSG' $ UPPER(lcString)
    			llInvalidAddress = .T.
    			IF EMPTY(lcMessage)
    				lcMessage = "The address you provided is not recognized by the US Postal Service as an address we serve. Mail sent to this address may be returned."
    			ELSE
    				lcMessage = lcMessage  + CHR(13) + "The address you provided is not recognized by the US Postal Service as an address we serve. Mail sent to this address may be returned."
    			ENDIF
    			
    		CASE 'CLASS="ERROR"' $ UPPER(lcString)
    			IF EMPTY(THIS.edtMessage.VALUE)
    				lcMessage  = SUBSTR(lcString, AT('class="error"', lcString) + 14, AT('</li>', lcString) - AT('class="error"', lcString) -14)
    			ELSE
    				lcMessage  = THIS.edtMessage.VALUE + CHR(13) + SUBSTR(lcString, AT('class="error"', lcString) + 14, AT('</li>', lcString) - AT('class="error"', lcString) -14)
    			ENDIF
    			
    	ENDCASE
    
    	IF EMPTY(lcString)
    		LOOP
    	ENDIF
    
    	IF !'CITY RANGE' $ UPPER(lcString)
    		LOOP
    	ENDIF
    
    	lcCity 	= SUBSTR(lcString, AT('city range',lcString) + 12, AT('</span>',lcString) - AT('city range',lcString)-12)
    	lcState 	= SUBSTR(lcString, AT('state range',lcString) +13, AT('</span> <span class="zip"', lcString) - AT('state range', lcString) -13)
    
    	IF "HYPHEN" $ UPPER(lcString)
    		lcZip 	= SUBSTR(lcString, AT('zip',lcString) +14, AT('<span class="hyphen">',lcString) - AT('zip',lcString) - 21)
    		lcZip4 	 = SUBSTR(lcString, AT('zip4',lcString) +6,4)
    	ELSE
    		lcZip 	= SUBSTR(lcString, AT('zip',lcString) +14, AT('</span><span class="zip4"', lcString) - AT('zip', lcString) - 14)
    		lcZip4 	= ""
    	ENDIF
    
    
    	lcString = laLines[lnIDx - 3]
    	lcStreet  = SUBSTR(lcString, AT('address1 range', lcString) + 16, AT('</span>', lcString) - AT('address1 range', lcString) - 16)
    	
    	lcAddress = lcStreet + CHR(13) +;
    				lcCity + ", " + lcState + " " + lcZip + IIF(EMPTY(lcZip4), "", "-" + lcZip4)
    	
    	MESSAGEBOX(lcAddress, 64, "System Message")			
    
    	EXIT
    
    ENDFOR
    
    USE IN SELECT('tmpHttp')
    


    Luis

    • Označen jako odpověď LazyCod3r 13. února 2017 17:30
    13. února 2017 17:30