运行Excel VBA代码时,出现错误消息:运行时错误’-2145320854(8021006a)?

huangapple go评论185阅读模式
英文:

When I try to run Excel VBA code, gives me error message: Runtime error '-2145320854 (8021006a)?

问题

以下是您要翻译的代码部分:

  1. Sub ExtractPhotoData()
  2. '声明变量
  3. Dim FSO As Object
  4. Dim SourceFolder As Object
  5. Dim FileItem As Object
  6. Dim Image As Object
  7. Dim RowCounter As Integer
  8. RowCounter = 1
  9. '设置文件系统对象和源文件夹
  10. Set FSO = CreateObject("Scripting.FileSystemObject")
  11. Set SourceFolder = FSO.GetFolder(Worksheets("Sheet2").Cells(2, 9).Value)
  12. '循环遍历源文件夹中的每个文件
  13. For Each FileItem In SourceFolder.Files
  14. '加载图像
  15. Set Image = CreateObject("WIA.ImageFile")
  16. Image.LoadFile FileItem.Path
  17. '提取经度、纬度和高度数据
  18. Dim Longitude As Long
  19. Dim Latitude As Long
  20. Dim Altitude As Long
  21. Longitude = Image.Properties("GPS Longitude").Value
  22. Latitude = Image.Properties("GPS Latitude").Value
  23. Altitude = Image.Properties("GPS Altitude").Value
  24. '将数据粘贴到工作表
  25. RowCounter = RowCounter + 1
  26. Cells(RowCounter, 1).Value = FileItem.Name
  27. Cells(RowCounter, 2).Value = Longitude
  28. Cells(RowCounter, 3).Value = Latitude
  29. Cells(RowCounter, 4).Value = Altitude
  30. Next FileItem
  31. End Sub
英文:

I've been trying to create this code using VBA to extract the GPS data from a set of image files in a folder and paste the data into a collection of rows in an excel table. I have no previous experience with VBA and am still a relative novice when it comes to the coding world in general so I have no clue what I'm doing but I made this code that I thought would work using random posts of similar things I found online. Now the issue I'm facing is the error code that is shown in the title of the post. I tried googling the issue but to no avail as the one link with a similar situation was never resolved. This project has taken up quite a bit of time and my boss, who I am terrified of, is starting to passive aggressively breath down my neck for it. I am asking for your assistance sharks, with an investment of giving me the solution, in exchange for 15% of my company, Save-Me-From-My-Scary-Boss Inc.

Here's the code I used:

  1. Sub ExtractPhotoData()
  2. 'Declare variables
  3. Dim FSO As Object
  4. Dim SourceFolder As Object
  5. Dim FileItem As Object
  6. Dim Image As Object
  7. Dim RowCounter As Integer
  8. RowCounter = 1
  9. 'Set up the file system object and source folder
  10. Set FSO = CreateObject("Scripting.FileSystemObject")
  11. Set SourceFolder = FSO.GetFolder(Worksheets("Sheet2").Cells(2, 9).Value)
  12. 'Loop through each file in the source folder
  13. For Each FileItem In SourceFolder.Files
  14. 'Load the image
  15. Set Image = CreateObject("WIA.ImageFile")
  16. Image.LoadFile FileItem.Path
  17. 'Extract the longitude, latitude, and altitude data
  18. Dim Longitude As Long
  19. Dim Latitude As Long
  20. Dim Altitude As Long
  21. Longitude = Image.Properties("GPS Longitude").Value
  22. Latitude = Image.Properties("GPS Latitude").Value
  23. Altitude = Image.Properties("GPS Altitude").Value
  24. 'Paste the data into the worksheet
  25. RowCounter = RowCounter + 1
  26. Cells(RowCounter, 1).Value = FileItem.Name
  27. Cells(RowCounter, 2).Value = Longitude
  28. Cells(RowCounter, 3).Value = Latitude
  29. Cells(RowCounter, 4).Value = Altitude
  30. Next FileItem
  31. End Sub

答案1

得分: 2

代码部分不翻译,以下是翻译好的内容:

"The error you get is 'Name not found'. You get it when you try to access a property that doesn't exist. Not all images have GPS information stored, so you should be prepared.

Also, as far as I know, the GPS-property names don't contain a space, and you need to be careful with the upper- vs lowercases. It should be Image.Properties("GpsLongitude") (see here)

You can get a list of all properties of an image with

  1. Dim p
  2. For Each p In Image.Properties
  3. Debug.Print FileItem.Name, p.Name
  4. Next

Next issue is that the properties for longitude and latitude don't return simple values. I checked with an image file and got an object of type 'IVector' for the values, containing 3 single values that define degree, minute and second, and you need additionally the property 'GpsLongitudeRef' to get N/S resp E/W.

For the altitude, you can read the value as 'Long', however there is more: the property contains, for example, also the unit ('m') and the 'GpsAltitudeRef' defines the reference ('Sea level').

I ended up with some example code

  1. On Error Resume Next
  2. Dim longitude As String
  3. longitude = getLongLat(Image.Properties("GpsLongitude").Value, Image.Properties("GpsLongitudeRef").Value)
  4. Dim latitude As String
  5. latitude = getLongLat(Image.Properties("GpsLatitude").Value, Image.Properties("GpsLatitudeRef").Value)
  6. Dim altitude As Long
  7. altitude = Image.Properties("GpsAltitude").Value
  8. On Error Resume Next
  9. Debug.Print FileItem.Name, longitude, latitude, altitude

and the helper function

  1. Function getLongLat(vector As Object, ref As String)
  2. On Error Resume Next
  3. getLongLat = vector(1) & ":" & vector(2) & ":" & vector(3) & " " & ref
  4. End Function

Of course, it's up to you to convert longitude and latitude into numbers.

英文:

The error you get is "Name not found". You get it when you try to access a property that doesn't exist. Not all images have GPS information stored, so you should be prepared.

Also, as far as I know, the GPS-property names doesn't contain a space, and you need to be careful with the upper- vs lowercases. It should be Image.Properties("GpsLongitude") (see here)

You can get a list of all properties of an image with

  1. Dim p
  2. For Each p In Image.Properties
  3. Debug.Print FileItem.Name, p.Name
  4. Next

Next issue is that the properties for longitude and latitude doesn't return simple values. I checked with an image file and got and object of type IVector for the values, containing 3 single values that define degree, minute and second, and you need additionally the property "GpsLongitudeRef" to get N/S resp E/W.

For the altitude, you can read the value as Long, however there is more: the property contains for example also the unit ("m") and the "GpsAltitudeRef" defines the reference ("Sea level").

I ended up with some example code

  1. On Error Resume Next
  2. Dim longitude As String
  3. longitude = getLongLat(Image.Properties("GpsLongitude").Value, Image.Properties("GpsLongitudeRef").Value)
  4. Dim latitude As String
  5. latitude = getLongLat(Image.Properties("GpsLatitude").Value, Image.Properties("GpsLatitudeRef").Value)
  6. Dim altitude As Long
  7. altitude = Image.Properties("GpsAltitude").Value
  8. On Error Resume Next
  9. Debug.Print FileItem.Name, longitude, latitude, altitude

and the helper function

  1. Function getLongLat(vector As Object, ref As String)
  2. On Error Resume Next
  3. getLongLat = vector(1) & ":" & vector(2) & ":" & vector(3) & " " & ref
  4. End Function

Of course it's up to you to convert longitude and latitude into numbers.

huangapple
  • 本文由 发表于 2023年3月21日 03:20:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794455-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定