+ Reply to Thread
Results 1 to 5 of 5

VBA Module - Google Map error

  1. #1
    Registered User
    Join Date
    04-09-2013
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    3

    VBA Module - Google Map error

    Hi,

    I inherited a module in the macro that has been giving me some problems:

    1. I get a compile error due to the users having different versions of Excel (IE. 32bit vs 64bit, 2010 vs 2016)
    2. The module script below doesn't always work, (IE. Some area codes won't come up and returns #Name error while others come back with the wrong distance)

    I was wondering if there's a better way to get the miles from one zip code in the US to another?

    Function GetDirections(Origin As String, Destination As String) As String

    ' Create a WebClient for executing requests
    ' and set a base url that all requests will be appended to

    Dim MapsClient As New WebClient

    MapsClient.BaseUrl = "https://maps.googleapis.com/maps/api/"

    ' Create a WebRequest for getting directions

    Dim DirectionsRequest As New WebRequest

    DirectionsRequest.Resource = "directions/{format}"

    DirectionsRequest.Method = HttpGet

    ' Set the request format -> Sets {format} segment, content-types, and parses the response

    DirectionsRequest.Format = Json

    ' (Alternatively, replace {format} segment directly)

    DirectionsRequest.AddUrlSegment "format", "json"

    ' Add parameters to the request (as querystring for GET calls and body otherwise)

    DirectionsRequest.AddQuerystringParam "origin", Origin

    DirectionsRequest.AddQuerystringParam "destination", Destination

    ' Force parameter as querystring for all requests

    DirectionsRequest.AddQuerystringParam "sensor", "false"

    ' => GET https://maps.../api/directions/json?....&sensor=false

    ' Execute the request and work with the response

    Dim Response As WebResponse

    Set Response = MapsClient.Execute(DirectionsRequest)

    If Response.StatusCode = WebStatusCode.Ok Then

    ' Work directly with parsed json data

    Dim Route As Object

    Set Route = Response.Data("routes")(1)("legs")(1)

    GetDirections = Route("distance")("text")

    Else

    GetDirections = "Error: " & Response.Content

    End If

    End Function

    Thank you!

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: VBA Module - Google Map error

    .
    The Forum server won't let me post the code or attachment here because it believes there is HTML code included.

    Here is a link to download a small project that might assist you. It provides mileage and time between two zip codes.

    Hope this helps.

    https://www.amazon.com/clouddrive/sh...mxy9imaS4GyeJG
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    04-09-2013
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    3

    Question Re: VBA Module - Google Map error

    Thanks for this, it's def better than what I have. I would need this version to work on a Mac as well, I'm coming up with the error message when I try to run this on a Mac.

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: VBA Module - Google Map error

    .
    So sorry. No Mac here. Not familiar.

  5. #5
    Registered User
    Join Date
    04-09-2013
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    3

    Thumbs up Re: VBA Module - Google Map error

    Ok, thanks for your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Calling a Module From Another Module (Error 1004)
    By js0873 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 07-17-2017, 12:19 PM
  2. Error Handler for Compile Error in Hidden Module
    By Jerbinator in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-29-2016, 01:56 PM
  3. Compile error in hidden module: Module 1 when workbook opened on different machine!?
    By MathUKTeacher in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-06-2015, 04:38 PM
  4. Distances using google API Runtime 91 error
    By robtuby in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-13-2014, 07:21 AM
  5. [SOLVED] Member already exists in an object module form which this object module derives error
    By Sc0tt1e in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-01-2014, 03:14 AM
  6. [SOLVED] Code locks cells when inserted in sheet module but returns error in standard module
    By yoda66 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-07-2014, 07:39 AM
  7. how to access Sheet module, normal module, Worbook module to type code
    By alibaba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2009, 07:51 AM

Tags for this Thread

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