R
Ron Rosenfeld
I am trying to develop a VBA function that will do a reverse zip code lookup at
the USPS site. I have a sub that steps through a range of zip codes, and
retrieves the appropriate city or cities in that zip code.
The code works fine initially, but after a while, it returns an error on the
set IE = New InternetExplorer
line. The error is an "Unknown Automation Error" with text:
-2147467259 (80004005) Unspecified error.
Changing to late binding (at least after the error messages start) do not make
a difference.
Closing and re-opening Excel doesn't help.
The only way I can resume normal operations, that I've found so far, is to
reboot my computer.
Excel 2007
XP Pro SP3
IE8
Any help appreciated. There is code that calls this function, but I don't
think it is relevant to the error message.
============================
'*** IMPORTANT NOTE ***
'MUST SET REFERENCE (TOOLS/REFERENCES IN MAIN MENU BAR)
'TO:
' Microsoft Internet Controls
' In Excel 2007, this is called "Microsoft Browser Helpers"
Option Explicit
Private Function RevZip(sZip5 As String) As Variant
Dim IE As InternetExplorer
Const sURL As String = "http://zip4.usps.com/zip4/citytown_zip.jsp"
Dim sHTML As String
Dim sTemp() As String
Dim i As Long
' Group2 = City Group3=State IGNORE CASE
Const rePattern As String = "headers=pre>(<b>)?([^,]+),\s([^<]+)"
Dim lNumCities As Long
'next line is highlighted when automation error occurs
Set IE = New InternetExplorer
IE.Navigate sURL
IE.Visible = False
Do While IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Do While IE.Busy = True
DoEvents
Loop
IE.Document.all("zip5").Value = sZip5
IE.Document.all("Submit").Click
Do While IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Do While IE.Busy = True
DoEvents
Loop
sHTML = IE.Document.body.innerhtml
Set IE = Nothing
lNumCities = RegexCount(sHTML, rePattern)
ReDim sTemp(0 To 1, 0 To lNumCities - 1)
For i = 0 To lNumCities - 1
sTemp(0, i) = RegexMid(sHTML, rePattern, i + 1, 2)
sTemp(1, i) = RegexMid(sHTML, rePattern, i + 1, 3)
Next i
RevZip = sTemp
End Function
==============================
--ron
the USPS site. I have a sub that steps through a range of zip codes, and
retrieves the appropriate city or cities in that zip code.
The code works fine initially, but after a while, it returns an error on the
set IE = New InternetExplorer
line. The error is an "Unknown Automation Error" with text:
-2147467259 (80004005) Unspecified error.
Changing to late binding (at least after the error messages start) do not make
a difference.
Closing and re-opening Excel doesn't help.
The only way I can resume normal operations, that I've found so far, is to
reboot my computer.
Excel 2007
XP Pro SP3
IE8
Any help appreciated. There is code that calls this function, but I don't
think it is relevant to the error message.
============================
'*** IMPORTANT NOTE ***
'MUST SET REFERENCE (TOOLS/REFERENCES IN MAIN MENU BAR)
'TO:
' Microsoft Internet Controls
' In Excel 2007, this is called "Microsoft Browser Helpers"
Option Explicit
Private Function RevZip(sZip5 As String) As Variant
Dim IE As InternetExplorer
Const sURL As String = "http://zip4.usps.com/zip4/citytown_zip.jsp"
Dim sHTML As String
Dim sTemp() As String
Dim i As Long
' Group2 = City Group3=State IGNORE CASE
Const rePattern As String = "headers=pre>(<b>)?([^,]+),\s([^<]+)"
Dim lNumCities As Long
'next line is highlighted when automation error occurs
Set IE = New InternetExplorer
IE.Navigate sURL
IE.Visible = False
Do While IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Do While IE.Busy = True
DoEvents
Loop
IE.Document.all("zip5").Value = sZip5
IE.Document.all("Submit").Click
Do While IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Do While IE.Busy = True
DoEvents
Loop
sHTML = IE.Document.body.innerhtml
Set IE = Nothing
lNumCities = RegexCount(sHTML, rePattern)
ReDim sTemp(0 To 1, 0 To lNumCities - 1)
For i = 0 To lNumCities - 1
sTemp(0, i) = RegexMid(sHTML, rePattern, i + 1, 2)
sTemp(1, i) = RegexMid(sHTML, rePattern, i + 1, 3)
Next i
RevZip = sTemp
End Function
==============================
--ron