ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > Excel VBA > 如何使用VBA获取相片拍照日期?

如何使用VBA获取相片拍照日期?

作者:绿色风 分类: 时间:2022-08-17 浏览:130
楼主
wise
Q:如何使用VBA获取相片拍照日期?
A:ALT+F11→插入模块→输入以下代码:

  1. Option Explicit
  2. Private Function FindPicDate(PicFile As String) As String
  3.     Dim bytes() As Byte
  4.     Dim sLine() As String
  5.     Dim fSize As Long, ExifDate As Long
  6.     Dim i As Long, d As Long
  7.     Dim ff      As Integer
  8.     Dim Found   As Boolean
  9.    
  10.     ff = FreeFile
  11.     fSize = FileLen(PicFile)
  12.      
  13.     If fSize > 1024 Then fSize = 1024
  14.     ReDim bytes(1 To fSize)
  15.     Open PicFile For Binary As #ff
  16.         Get #ff, 1, bytes
  17.     Close ff
  18.       
  19.     sLine = Split(StrConv(bytes(), vbUnicode), Chr$(0))
  20.    
  21.     For i = 0 To UBound(sLine)
  22.         ExifDate = InStr(1, sLine(i), "xif")
  23.         If ExifDate > 0 Then
  24.             Found = True
  25.             Exit For
  26.         End If
  27.     Next i
  28.         
  29.     If Found = False Then Exit Function
  30.    
  31.     For d = i + 1 To UBound(sLine)
  32.         ExifDate = InStr(1, sLine(d), ":")
  33.         If ExifDate > 0 Then
  34.             FindPicDate = sLine(d)
  35.             Exit For
  36.         End If
  37.     Next d
  38.             
  39. End Function
  40. Sub test()
  41. MsgBox "照片创建日期为:" & FindPicDate(ThisWorkbook.Path & "\IMG_0118.JPG")
  42. End Sub


VBA获取拍照日期.rar
2楼
wise
xyh9999后来追加的解法:
  1. Function Lqc_GetPicDate(PicFile As String) '提取照片日期
  2. '例如:? Lqc_GetPicDate("c:\J002.jpg")
  3.     On Error Resume Next
  4.     Dim bytes() As Byte, sLine() As String, fSize As Long, ExifDate As Long, i As Long, d As Long, ff  As Integer, Found As Boolean, TmpCC As String, reg As Object
  5.     ff = FreeFile: fSize = FileLen(PicFile)
  6.     If fSize > 1024 Then fSize = 1024 ' get 1st 1K of file.
  7.     ReDim bytes(1 To fSize)
  8.     Set reg = CreateObject("VBScript.RegExp")
  9.     reg.Global = True
  10.     reg.Pattern = "\d{4}:\d{2}:\d{2}\s{1,3}\d{2}:\d{2}:\d{2}"
  11.     Open PicFile For Binary As #ff
  12.         Get #ff, 1, bytes
  13.     Close ff
  14.       
  15.     sLine = Split(StrConv(bytes(), vbUnicode), Chr$(0))
  16.    
  17.     For i = 0 To UBound(sLine) ' does "Exif" exsist?
  18.         ExifDate = InStr(1, sLine(i), "xif")    '这里本来应该是Exif
  19.         If ExifDate > 0 Then
  20.             Found = True
  21.             Exit For
  22.         End If
  23.     Next i
  24.         
  25.     If Found = False Then Exit Function ' return nothing, "Exif" not found!
  26.    
  27.     TmpCC = ""
  28.     For d = i + 1 To UBound(sLine) ' find first ":" in file
  29.         ExifDate = InStr(1, sLine(d), ":")
  30.         If ExifDate > 0 Then
  31.             TmpCC = TmpCC & "|" & sLine(d) ' return date string
  32.         End If
  33.     Next d
  34.    
  35.     Set cArr = reg.Execute(TmpCC)
  36.     If cArr Is Nothing Then Exit Function
  37.     If cArr.Count >= 2 Then
  38.        Lqc_GetPicDate = cArr(1).Value '取第2个日期
  39.     Else
  40.        If cArr.Count = 1 Then
  41.             Lqc_GetPicDate = cArr(0).Value '取第1个日期
  42.        Else
  43.        End If
  44.     End If
  45.     Set reg = Nothing
  46. End Function
3楼
Idiot
这个代友怎么用啊
4楼
Idiot
如何实现在EXCEL A列显示指定文件夹中所有图片文件名,B列中显示所有图片的拍摄时间啊

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap