Option Explicit
'
'Google Maps Driving Times
'
' ################################################################################
Sub test()
MsgBox TgetGoogDistanceTime(Range("A2"), Range("B2"), "time")
MsgBox VgetGoogDistanceTime(Range("A2"), Range("B2"), "time")
End Sub
' ################################################################################
'Separate distance and time - text output
'shred dude vbax
Public Function TgetGoogDistanceTime(rngSAdd As Range, rngEAdd As Range, Optional strReturn As String = "distance") As String
' =TGetGoogDistanceTime($A$1,$A$2,"time")
' coventry manchester 2 hours 5 mins
' =TGetGoogDistanceTime($A$1,$A$2,"distance")
' coventry manchester 116 mi
Dim sURL As String
Dim BodyTxt As String
sURL = "http://maps.google.com/maps?f=d&source=s_d"
sURL = sURL & "&saddr=" & Replace(rngSAdd(1).Value, " ", "+")
sURL = sURL & "&daddr=" & Replace(rngEAdd(1).Value, " ", "+")
sURL = sURL & "&hl=en"
BodyTxt = getHTML(sURL)
If InStr(1, BodyTxt, strReturn, vbTextCompare) = 0 Then
TgetGoogDistanceTime = "Error"
Else
TgetGoogDistanceTime = parseGoog(strReturn, BodyTxt)
End If
End Function
' ################################################################################
'Separate distance and time - not text
'shred dude vbax
Public Function VgetGoogDistanceTime( _
rngSAdd As Range, _
rngEAdd As Range, _
Optional strReturn As String = "distance") _
As Variant
' =VGetGoogDistanceTime($A$1,$A$2,"time")
' coventry manchester 02:05
' =VGetGoogDistanceTime($A$1,$A$2,"distance")
' coventry manchester 116
Dim sURL As String
Dim BodyTxt As String
sURL = "http://maps.google.com/maps?f=d&source=s_d"
sURL = sURL & "&saddr=" & Replace(rngSAdd(1).Value, " ", "+")
sURL = sURL & "&daddr=" & Replace(rngEAdd(1).Value, " ", "+")
sURL = sURL & "&hl=en"
BodyTxt = getHTML(sURL)
If InStr(1, BodyTxt, strReturn, vbTextCompare) = 0 Then
VgetGoogDistanceTime = "Error"
Else
VgetGoogDistanceTime = parseGoog(strReturn, BodyTxt)
If LCase(strReturn) Like "time*" Then
If InStr(1, VgetGoogDistanceTime, "hours", vbTextCompare) <> 0 Then
VgetGoogDistanceTime = Evaluate("""" & Replace(Replace(VgetGoogDistanceTime, " hours ", ":"), " mins", "") & ":0.0" & """+0")
Else
VgetGoogDistanceTime = Evaluate("""" & Replace(Replace(VgetGoogDistanceTime, " hour ", ":"), " mins", "") & ":0.0" & """+0")
End If
Else
VgetGoogDistanceTime = Val(VgetGoogDistanceTime)
End If
End If
End Function
' ################################################################################
Public Function getHTML(strURL As String) As String
'Returns the HTML code underlying a given URL
Dim oXH As Object
Set oXH = CreateObject("msxml2.xmlhttp")
With oXH
.Open "get", strURL, False
.Send
getHTML = .responseText
End With
Set oXH = Nothing
End Function
' ################################################################################
Public Function parseGoog(strSearch As String, strHTML As String) As String
strSearch = strSearch & ":"""
If InStr(1, strHTML, strSearch) = 0 Then parseGoog = "Not Found": Exit Function
parseGoog = Mid(strHTML, InStr(1, strHTML, strSearch) + Len(strSearch))
parseGoog = Mid(parseGoog, 1, InStr(1, parseGoog, """") - 1)
End Function
' ################################################################################
Bookmarks