VBA on Mac: using QueryTables.Add to import data form Web using API but wrong import coding (macintosh instead of utf-8

30 Views Asked by At

I'm working with a Mac and I'm using Excel 365.

I have the following task: I need to retrive from the web the State and the Country from an Address stored in a worksheet To do It i'm using Google Geocode API.

The problem is the following one:

In VBA I have 2 procedures:

the first one that create a iqy file to use after and when I define the api query:

Private Sub CreateFileIQY(Address As String)

    Dim filePath As String
    Dim fileContent As String
    Dim API_KEY As String
    
    API_KEY = "MY_KEY"
    filePath = ThisWorkbook.Path & "/query.iqy"
    fileContent = "https://maps.googleapis.com/maps/api/geocode/json?address=""" & Address & """&key=" & API_KEY & "&language=en-GB"
    
    Open filePath For Output As #1
    Print #1, fileContent
    Close #1
    
End Sub

the second one that run the query and put the result in the spreadsheet "JSON" in a specific column:

Private Sub DownloadJSON(col As Integer)

    Dim filePath As String
    Dim fileContent As String
    Dim JSONws As Worksheet
    
    filePath = ThisWorkbook.Path & "/query.iqy"
    Set JSONws = ThisWorkbook.Worksheets("JSON")

    Application.CutCopyMode = False
    With JSONws.QueryTables.Add(Connection:= _
        "FINDER;" & filePath, Destination:=JSONws.Cells(3, col))
        .Name = "Geocode"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = False
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .WebSelectionType = xlAllTables
        .WebFormatting = xlWebFormattingAll
        .WebPreFormattedTextToColumns = False
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = True
        .Refresh BackgroundQuery:=False
    End With
    
End Sub

I have the following problem:

if I try to search "Turchia" (in Italian) using this query: https://maps.googleapis.com/maps/api/geocode/json?address=turchia&key=MYKEY&sensor=false&language=en-GB

I see the following JSON in Chrome:

{
   "results" : 
   [
      {
         "address_components" : 
         [
            {
               "long_name" : "Türkiye",
               "short_name" : "TR",
               "types" : 
               [
                  "country",
                  "political"
               ]
            }
         ],
         "formatted_address" : "Türkiye",
         "geometry" : 
         {
            "bounds" : 
            {
               "northeast" : 
               {
                  "lat" : 42.3666999,
                  "lng" : 44.8178449
               },
               "southwest" : 
               {
                  "lat" : 35.8085919,
                  "lng" : 25.4408142
               }
            },
            "location" : 
            {
               "lat" : 38.963745,
               "lng" : 35.243322
            },
            "location_type" : "APPROXIMATE",
            "viewport" : 
            {
               "northeast" : 
               {
                  "lat" : 42.3666999,
                  "lng" : 44.8178449
               },
               "southwest" : 
               {
                  "lat" : 35.8085919,
                  "lng" : 25.4408142
               }
            }
         },
         "place_id" : "ChIJcSZPllwVsBQRKl9iKtTb2UA",
         "types" : 
         [
            "country",
            "political"
         ]
      }
   ],
   "status" : "OK"
}

As you can see the reported name is "Türkiye".

But after the VBA execution I see in the spreadsheet "JSON" the name "Türkiye".

This is due to the fact that the original JSON retrived by the web is coded with UTF-8, but when the QueryTables.Add run it import the data with the "macintosh" code.

Can you help me to resolve this issue? I tried to:

  • change the API configuration but there isn't an option to have onliu ASCII caracter
  • create a funcion in VBA that convert the "macintosh" import in a "UTF-8" import but all the solutions that I used doesn't works

Thanks.

I espected:

  • a chenage in QueryTables function that allow me to import the data in the correct way
  • a function that allow me to convert the import in the right format
0

There are 0 best solutions below