Results 1 to 1 of 1

Use google maps to get distance

Threaded View

rdr910 I would like to ultimately be... 10-13-2010, 09:16 PM
  1. #1
    Registered User
    Join Date
    10-13-2010
    Location
    sc
    MS-Off Ver
    Excel 2007
    Posts
    4
    I would like to ultimately be able to get the mileage distance from google maps by entering both a location and destination in Excel. If the mileage could be outputted back into Excel. I am not sure of how to go about this, since i'm new to all of this, i have not done any programming in a good 10 years. If I could possibly get some help on this and pointed in the right direction.

    If it is possible to get the mileage outputted. I have already setup all my calculations i would need to be used with the mileage.

    I am trying to get the distance from mapquest after I enter in the To and From location in Excel. First I enter in the information listed below.

    Cell A1 - Address
    B1 - City
    C1 = State

    D1 - Address
    E1 - City
    F1 - State

    I type in the information in excel and run the program but keep getting a compiling error. Can anyone please help? The code I have is listed below.
    Public Function GetDistance(startAddr As String, startCity As String, _
      startState As String, startZip As String, endAddr As String, _
      endCity As String, endState As String, endZip As String) As String
    
    Dim sURL As String
    Dim appIE As InternetExplorer
    Dim regex As RegExp, Regmatch As MatchCollection
    Dim BodyTxt As String
    Dim GetFirstPos As Long
    
    sURL = "http://www.mapquest.com/maps?1c=" & Replace(startCity, " ", "+")
    sURL = sURL & "&1s=" & startState & "&1a=" & Replace(startAddr, " ", "+")
    sURL = sURL & "&1z=" & startZip & "&2c=" & endCity & "&2s=" & endState
    sURL = sURL & "&2a=" & Replace(endAddr, " ", "+") & "&2z=" & endZip
    
    Set appIE = New InternetExplorer
    'Set appIE = CreateObject("Internetexplorer.application")
    
    appIE.navigate sURL
    appIE.Visible = True
    
        Do
            DoEvents
        Loop Until appIE.readyState = READYSTATE_COMPLETE
    
    appIE.Refresh
    
    Set regex = New RegExp
    With regex
        .Pattern = "Total Estimated Distance"
        .MultiLine = False
    End With
    
    BodyTxt = appIE.document.Body.innerText
    Set Regmatch = regex.Execute(BodyTxt)
    
    If Regmatch.Count > 0 Then
        GetFirstPos = WorksheetFunction.Find("Total Estimated Distance", BodyTxt, 1)
        
        GetDistance = Mid$(BodyTxt, GetFirstPos, 30)
     
        Else
        GetDistance = "Address Error, fix and try again"
    End If
    
    appIE.Quit
    Set appIE = Nothing
    Set regex = Nothing
    Set Regmatch = Nothing
    
    End Function
    Last edited by Leith Ross; 10-19-2010 at 10:01 PM. Reason: Merged posts

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1