|
楼主试试,下面的代码在我的电脑上修改成功:
自定义函数:
- Function ModiPhotoDate(strPath As Variant, strFile As String, strNewDate As String)
- 'strPath:文件所在文件夹名,必须是Variant类型
- 'strFile:文件名(不包括路径)
- 'strNewDate:要显示的日期时间,yyyy-mm-dd hh:nn格式,时间不写的话用原照片时间
-
- Dim objShell As Object
- Dim objFolder As Object
- Dim objFolderItem As Object
- Dim strOldDate As String
- Dim arrOldDate() As Byte
- Dim arrNewDate() As Byte
- Dim arrByte() As Byte
- Dim strNewFile As String
- Dim i As Long
- Dim j As Long
-
- '照片复件文件名
- strNewFile = Left(strFile, InStrRev(strFile, ".") - 1) & "复件." & Mid(strFile, InStrRev(strFile, ".") + 1)
-
- '获取照片的拍摄日期时间
- Set objShell = CreateObject("Shell.application")
- Set objFolder = objShell.Namespace(strPath)
- Set objFolderItem = objFolder.ParseName(strFile)
- '第25项是照片的拍摄日期,不知道不同的配置是否相同
- strOldDate = objFolder.GetDetailsOf(objFolderItem, 25)
- Set objFolderItem = Nothing
- Set objFolder = Nothing
- Set objShell = Nothing
-
- If Len(strNewDate) <= 11 Then
- strNewDate = Trim(strNewDate) & Format(strOldDate, " hh:nn")
- End If
-
- '日期在照片二进制中是"yyyy:mm:dd hh:nn:ss"格式存在,不知道不同格式的照片是否相同
- arrOldDate = StrConv(Format(strOldDate, "yyyy:mm:dd hh:nn"), vbFromUnicode)
- arrNewDate = StrConv(Format(strNewDate, "yyyy:mm:dd hh:nn"), vbFromUnicode)
-
- '读入照片的二进制数据
- With CreateObject("Adodb.Stream")
- .Open
- .Type = 1 'adTypeBinary
- .LoadFromFile strPath & "" & strFile
- arrByte = .read
- .Close
- End With
-
- '修改日期时间
- For i = 0 To UBound(arrByte)
- If arrByte(i) = arrOldDate(0) Then
- For j = 1 To UBound(arrOldDate)
- If arrByte(i + j) <> arrOldDate(j) Then Exit For
- Next
- If j > UBound(arrOldDate) Then
- For j = 0 To UBound(arrOldDate)
- arrByte(i + j) = arrNewDate(j)
- Next
- End If
- End If
- Next
-
- '二进制数据写入文件,创建日期和修改日期等是当前日期
- With CreateObject("Adodb.Stream")
- .Type = 1 'adTypeBinary
- .Open
- .Write arrByte
- .SaveToFile strPath & "" & strNewFile, 2 'adSaveCreateOverWrite
- .Close
- End With
-
- End Function
-
复制代码 调用方法:
Sub Main()
Call ModiPhotoDate("D:\新建文件\资料", "123.jpg", "2014-12-01")
End Sub |
评分
-
1
查看全部评分
-
|