+ Reply to Thread
Results 1 to 7 of 7

Add Geolocation to Images from Excel

Hybrid View

  1. #1
    Registered User
    Join Date
    11-26-2019
    Location
    Thane
    MS-Off Ver
    2021
    Posts
    49

    Add Geolocation to Images from Excel

    Dear experts,

    I have multiple images stored in a folder, and their names are listed in column A of my Excel sheet. I would like to add geo-location data to these images using Excel VBA.
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,545

    Re: Add Geolocation to Images from Excel

    Try:
    Sub Test()
        Dim lRow As Long
        Dim i As Long
        Dim oWIA As Object
    
        lRow = Cells(Rows.Count, "A").End(xlUp).Row
        Set oWIA = CreateObject("WIA.ImageFile")
    
        With oWIA
            For i = 2 To lRow
                .LoadFile "e:\MojePobrane\" & Cells(i, "A").Value & ".jpg"
                With .Properties("GpsLatitude").Value
                    Cells(i, 2).Value = .Item(1).Value + .Item(2).Value / 60 + .Item(3).Value / 3600
                End With
                With .Properties("GpsLongitude").Value
                    Cells(i, 3).Value = .Item(1).Value + .Item(2).Value / 60 + .Item(3).Value / 3600
                End With
            Next i
        End With
    
    End Sub
    Artik

  3. #3
    Registered User
    Join Date
    11-26-2019
    Location
    Thane
    MS-Off Ver
    2021
    Posts
    49

    Re: Add Geolocation to Images from Excel

    Thanks @Artik

    I think i made some confusion, i want excel latitude longitude to image file.

  4. #4
    Registered User
    Join Date
    03-12-2022
    Location
    Argentina
    MS-Off Ver
    2016
    Posts
    38

    Re: Add Geolocation to Images from Excel

    hi,
    In the example below, select the image and then the macro inserts the geolocation and date that are embedded in the image, in the bottom left corner of the image, and then saves the image to the desktop with the same name.

    putlatitudeandlongitudeinimage.xlsm

  5. #5
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,187

    Re: Add Geolocation to Images from Excel

    The GPS datas are found in the EXIF properties of an image file and can be read if exists by WIA (Windows Image Acquisition)

    If not exists, can be added by third party programs such as "ExifTool" by Phil Harvey (https://exiftool.org/)

    But, if you like to have a simple solution by VBA using the WIA object; the following simple code will add the GPS data in the "Comments" section of the JPG file as seen in the picture below.



    The code will create a new file in the same directory with a file name starting with "New_***"

    Sub Test()
    '   Haluk - 29/03/2024
        Dim Lat As String, Lon As String, Img As Object, IP As Object, V As Object, myFile As Variant, NewFile As String
        
        Lat = "39°55'29,58"" N"
        Lon = "32°50'12.49"" E"
        
        Set Img = CreateObject("WIA.ImageFile")
        Set IP = CreateObject("WIA.ImageProcess")
        Set V = CreateObject("WIA.Vector")
        
        myFile = Application.GetOpenFilename("Image Files (*.jpg), *.jpg")
        If myFile = False Then Exit Sub
        
        Img.LoadFile myFile
        
        IP.Filters.Add (IP.FilterInfos("Exif").FilterID)
        IP.Filters(1).Properties("ID") = 40092
        IP.Filters(1).Properties("Type") = 1101
        
        V.SetFromString (Lat & " - " & Lon)
        
        IP.Filters(1).Properties("Value") = V
        
        Set Img = IP.Apply(Img)
        
        NewFile = "New_" & Dir(myFile)
        NewFile = Replace(myFile, Dir(myFile), NewFile)
        Img.SaveFile NewFile
    End Sub
    Attached Images Attached Images
    Last edited by Haluk; 03-29-2024 at 03:32 AM.

  6. #6
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,187

    Re: Add Geolocation to Images from Excel

    I worked on the subject and finally managed to add "Longitude" data (decimal 24.858) to the image file's GPS section as seen in the picture below, by the following code.

    Sub TestLongitude()
    '   Haluk - 31/03/2024
    '   Reference : Microsoft Windows Image Acquistion Library V2.0
        
        Dim Longitude As Double, Img As WIA.ImageFile, IP As WIA.ImageProcess, V As WIA.Vector
        Dim r1 As New Rational, r2 As New Rational, r3 As New Rational
        Dim myFile As Variant, NewFile As String
        Dim Deg As Integer, Min As Double, Sec As Double
        
        Longitude = 24.858
            
        Set Img = New WIA.ImageFile
        Set IP = New WIA.ImageProcess
        Set V = New WIA.Vector
        
        myFile = Application.GetOpenFilename("Image Files (*.jpg), *.jpg")
        If myFile = False Then Exit Sub
        
        NewFile = "New_" & Dir(myFile)
        NewFile = Replace(myFile, Dir(myFile), NewFile)
        
        If Dir(NewFile) <> "" Then Kill NewFile
        
        Img.LoadFile myFile
        
        IP.Filters.Add (IP.FilterInfos("Exif").FilterID)
        IP.Filters(1).Properties("ID") = 4
        IP.Filters(1).Properties("Type") = 1106
        
        Call Convert_LatLon(Longitude, Deg, Min, Sec)
        
    '   Set longitude as vector of unsigned rationals
    
        r1.Numerator = Deg
        r1.Denominator = 1
        
        r2.Numerator = Int(Min)
        r2.Denominator = 1
     
        r3.Numerator = Sec
        r3.Denominator = 1
        
        V.Add r1, 0
        V.Add r2, 0
        V.Add r3, 0
        
        IP.Filters(1).Properties("Value") = V
        
        Set Img = IP.Apply(Img)
           
        NewFile = "New_" & Dir(myFile)
        NewFile = Replace(myFile, Dir(myFile), NewFile)
        Img.SaveFile NewFile
    End Sub
    '
    Function Convert_LatLon(Decimal_Deg As Double, ByRef Degrees As Integer, ByRef Minutes As Double, ByRef Seconds As Double)
        Degrees = Int(Decimal_Deg)
        Minutes = (Decimal_Deg - Degrees) * 60
        Seconds = (Minutes - Int(Minutes)) * 60
    End Function

    Latitude and altitude datas can also be added by using appropriate values and similar methods.
    Attached Images Attached Images

  7. #7
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,187

    Re: Add Geolocation to Images from Excel

    @ rajatds31,

    Seems you have lost your interest on the subject

+ 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. GeoLocation Sorting
    By nathanthomas78 in forum Excel General
    Replies: 3
    Last Post: 09-17-2021, 10:17 AM
  2. Fetch Geolocation of Device
    By Afpb505 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-04-2021, 11:41 AM
  3. [SOLVED] Saved Images in Google Drive used for Dynamic Images in Excel
    By gtnilc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-10-2021, 02:35 AM
  4. Replies: 7
    Last Post: 05-28-2020, 08:33 AM
  5. Replies: 0
    Last Post: 01-01-2014, 04:36 PM
  6. GeoLocation data (Lat/Lon)
    By bstubbs in forum Excel General
    Replies: 4
    Last Post: 01-13-2011, 02:40 PM
  7. GeoLocation data (Lat/Lon) crossStreet
    By bstubbs in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-10-2009, 01:01 PM

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