Option Compare Database
Option Explicit
#If VBA7 Then
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) ' For 64 Bit Systems
Public Declare PtrSafe Function DeleteUrlCacheEntry Lib "wininet" Alias "DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As Long
#Else
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) ' For 32 Bit Systems
Public Declare Function DeleteUrlCacheEntry Lib "wininet" Alias "DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As Long
#End If
Public Function seachPostcode(ByVal txtAddress As String) As String
Dim ie As Object
Dim tempStr As String
Dim myArray() As String
Dim i As Integer
Dim myDict
Dim varItem As Variant
On Error Resume Next
seachPostcode = ""
Set ie = CreateObject("Internetexplorer.application")
' invisible Internet Explorer
ie.Visible = False
ie.navigate "https://www.192.com/places/"
While ie.busy
DoEvents
Wend
ie.Document.getElementById("where_location").Value = txtAddress
' press button
ie.Document.Forms(0).submit
While ie.busy
DoEvents
Wend
Sleep (50)
tempStr = Trim(ie.Document.getElementById("ont-result-items").innerText)
ie.Quit
Set ie = Nothing
myArray = Split(tempStr, "Postcode details")
If UBound(myArray) > 0 Then
' create an object and assign a link to it a variable
Set myDict = CreateObject("Scripting.Dictionary")
For i = LBound(myArray) To UBound(myArray) - 1
While myArray(i) Like (vbNewLine & "*")
myArray(i) = Trim(Right(myArray(i), Len(myArray(i)) - 2))
Wend
While myArray(i) Like ("*" & vbNewLine)
myArray(i) = Trim(Left(myArray(i), Len(myArray(i)) - 2))
Wend
tempStr = Split(myArray(i), vbNewLine)(0)
If UBound(Split(tempStr, ",")) > 0 Then
tempStr = Split(tempStr, ",")(1)
End If
tempStr = Trim(tempStr)
myDict.Item(tempStr) = tempStr
Next
tempStr = ""
For Each varItem In myDict.Items '
tempStr = tempStr & varItem & vbNewLine
Next
seachPostcode = tempStr
Set myDict = Nothing
End If
Call DeleteUrlCacheEntry("https://www.192.com/places/")
End Function