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

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

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

问题

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

Sub ExtractPhotoData()

'声明变量
Dim FSO As Object
Dim SourceFolder As Object
Dim FileItem As Object
Dim Image As Object
Dim RowCounter As Integer

RowCounter = 1

'设置文件系统对象和源文件夹
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FSO.GetFolder(Worksheets("Sheet2").Cells(2, 9).Value)

'循环遍历源文件夹中的每个文件
For Each FileItem In SourceFolder.Files

    '加载图像
    Set Image = CreateObject("WIA.ImageFile")
    Image.LoadFile FileItem.Path

    '提取经度、纬度和高度数据
    Dim Longitude As Long
    Dim Latitude As Long
    Dim Altitude As Long
    Longitude = Image.Properties("GPS Longitude").Value
    Latitude = Image.Properties("GPS Latitude").Value
    Altitude = Image.Properties("GPS Altitude").Value

    '将数据粘贴到工作表
    RowCounter = RowCounter + 1
    Cells(RowCounter, 1).Value = FileItem.Name
    Cells(RowCounter, 2).Value = Longitude
    Cells(RowCounter, 3).Value = Latitude
    Cells(RowCounter, 4).Value = Altitude

Next FileItem

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:

Sub ExtractPhotoData()

'Declare variables
Dim FSO As Object
Dim SourceFolder As Object
Dim FileItem As Object
Dim Image As Object
Dim RowCounter As Integer

RowCounter = 1

'Set up the file system object and source folder
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FSO.GetFolder(Worksheets("Sheet2").Cells(2, 9).Value)

'Loop through each file in the source folder
For Each FileItem In SourceFolder.Files


    'Load the image
    Set Image = CreateObject("WIA.ImageFile")
    Image.LoadFile FileItem.Path
        
    'Extract the longitude, latitude, and altitude data
    Dim Longitude As Long
    Dim Latitude As Long
    Dim Altitude As Long
    Longitude = Image.Properties("GPS Longitude").Value
    Latitude = Image.Properties("GPS Latitude").Value
    Altitude = Image.Properties("GPS Altitude").Value
        
    'Paste the data into the worksheet
    RowCounter = RowCounter + 1
    Cells(RowCounter, 1).Value = FileItem.Name
    Cells(RowCounter, 2).Value = Longitude
    Cells(RowCounter, 3).Value = Latitude
    Cells(RowCounter, 4).Value = Altitude

Next FileItem

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

Dim p
For Each p In Image.Properties
    Debug.Print FileItem.Name, p.Name
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

On Error Resume Next
Dim longitude As String
longitude = getLongLat(Image.Properties("GpsLongitude").Value, Image.Properties("GpsLongitudeRef").Value)
Dim latitude As String
latitude = getLongLat(Image.Properties("GpsLatitude").Value, Image.Properties("GpsLatitudeRef").Value)

Dim altitude As Long
altitude = Image.Properties("GpsAltitude").Value
On Error Resume Next

Debug.Print FileItem.Name, longitude, latitude, altitude

and the helper function

Function getLongLat(vector As Object, ref As String)
    On Error Resume Next
    getLongLat = vector(1) & ":" & vector(2) & ":" & vector(3) & " " & ref
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

Dim p
For Each p In Image.Properties
    Debug.Print FileItem.Name, p.Name
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

On Error Resume Next
Dim longitude As String
longitude = getLongLat(Image.Properties("GpsLongitude").Value, Image.Properties("GpsLongitudeRef").Value)
Dim latitude As String
latitude = getLongLat(Image.Properties("GpsLatitude").Value, Image.Properties("GpsLatitudeRef").Value)

Dim altitude As Long
altitude = Image.Properties("GpsAltitude").Value
On Error Resume Next

Debug.Print FileItem.Name, longitude, latitude, altitude

and the helper function

Function getLongLat(vector As Object, ref As String)
    On Error Resume Next
    getLongLat = vector(1) & ":" & vector(2) & ":" & vector(3) & " " & ref
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:

确定