Hi, Thanks for the speedy reply, I am trying to adjust the code i have and am not able to get it working. i am quite new to vba and am struggling to get it working.
i have added
sURL = "https://maps.googleapis.com/maps/api/distancematrix/xml?key="My API KEY"&origins=" & _
and now i get an error on
oHttp.Open "GET", sURL, False
Please see full code below
Public Function GetDistanceBetweenTwoZips(sOrigin As String _
, sDestination As String _
, Optional sUnits As String = "M") As String
Dim oHttp As Object
Dim sURL As String, sHTML As String
Dim sDistance As String
Dim sDuration As String
Dim lTopicstart As Long, lTopicend As Long
On Error GoTo GetDistanceBetweenTwoZips_Error
'sOrigin = "77074"
'sDestination = "24112"
If sUnits = "K" Then
sUnits = "metric" 'kilometres is an option
Else
sUnits = "imperial" 'miles is the default
End If
'GMap webservice with XML output
sURL = "https://maps.googleapis.com/maps/api/distancematrix/xml?key="My API KEY"&origins=" & _
sURL = sURL & URLEncode(sOrigin) & "&destinations=" & URLEncode(sDestination)
sURL = sURL & "&units=" & sUnits & "&sensor=false"
'sURL = sURL & "&units=imperial&sensor=false" 'original code
'Debug.Print sURL
' Create an XMLHTTP object and add some error trapping
On Error Resume Next
Set oHttp = CreateObject("MSXML2.XMLHTTP")
'Set oHttp = CreateObject("MSXML.XMLHTTPRequest")
On Error GoTo 0
If oHttp Is Nothing Then
MsgBox "For some reason I wasn?t able to make a MSXML2.XMLHTTP object"""
GoTo GetDistanceBetweenTwoZips_Error
End If
'Open the URL in browser object
oHttp.Open "GET", sURL, False
'Debug.Print sURL
oHttp.send
sHTML = oHttp.responseText
Set oHttp = Nothing
' Formatted Distance
'Debug.Print sHTML
' Parse the returned xml to get the Distance
' actually need the string between <text> and </text>
lTopicstart = InStr(sHTML, "<distance>")
lTopicend = InStr(sHTML, "</distance>")
If lTopicstart = 0 And lTopicend = 0 Then
On Error Resume Next
Err.Raise 601, , "JED---No Distance returned by GoogleMap - perhaps invalid zipcode" 'added because some coords don't return Distance
GoTo GetDistanceBetweenTwoZips_Error
Else
sDistance = Trim(Mid(sHTML, lTopicstart + 10, lTopicend - lltopicstart - 10))
'Debug.Print Len(sDistance) & "[" & sDistance & "]"
lTopicstart = InStr(sDistance, "<text>")
lTopicend = InStr(sDistance, "</text>")
sDistance = Mid(sDistance, lTopicstart + 6, lTopicend - lTopicstart - 6)
Debug.Print "1--------- sDistance " & sDistance
'GetDistanceBetweenTwoZips = sDistance
End If
'
'Parse the xml to get the Duration (Time)
lTopicstart = InStr(sHTML, "<duration>")
lTopicend = InStr(sHTML, "</duration>")
If lTopicstart = 0 And lTopicend = 0 Then
On Error Resume Next
Err.Raise 602, , "JED---No Duration returned by GoogleMap" 'added because some coords don't return Duration
GoTo GetDistanceBetweenTwoZips_Error
Else
sDuration = Trim(Mid(sHTML, lTopicstart + 10, lTopicend - lltopicstart - 10))
'Debug.Print Len(sDuration) & "[" & sDuration & "]"
lTopicstart = InStr(sDuration, "<text>")
lTopicend = InStr(sDuration, "</text>")
sDuration = Mid(sDuration, lTopicstart + 6, lTopicend - lTopicstart - 6)
Debug.Print "2------ sDuration " & sDuration
End If
'Create the final output of the function
GetDistanceBetweenTwoZips = sDistance & "|" & sDuration
'Debug.Print GetDistanceBetweenTwoZips
'===========
On Error GoTo 0
Exit Function
GetDistanceBetweenTwoZips_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetDistanceBetweenTwoZips of Module GoogleMapsEtc"
End Function
If there is anyway to fix this code and get it working again i would be much appreicative as my whole date base has been developed rounf this code..
Thank you so much for your help.......