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.
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.
Try:Artik![]()
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
Thanks @Artik
I think i made some confusion, i want excel latitude longitude to image file.
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
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
Last edited by Haluk; 03-29-2024 at 03:32 AM.
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.
@ rajatds31,
Seems you have lost your interest on the subject
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks