So I wanted to wrap up my exploration of WIA and image Exif metadata by showing you how you can use WIA to not only read values, but also set them!
If you missed my previous post regarding WIA and Exif metadata, here are a few links:
The Code
As with my other examples, the code here is nothing too complicated (once you know what to do).
'---------------------------------------------------------------------------------------
' Procedure : WIA_SetExifPropety
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Set an image's Exif metadata property value
' Currently only for Ascii properties
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Late Binding version -> None required
' Early Binding version -> Microsoft Windows Image Acquisition Library vX.X
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sInitialImage : Fully qualified path and filename of the image file to set the Exif
' metadata property value
' lPropertyId : ID no of the Exif metadata property to set the value of
' vValue : Value to set
' sDestiationImage : Fully qualified path and filename if you wish to create a new image
' instead of overwriting the existing one
'
' Usage:
' ~~~~~~
' Call WIA_SetExifPropety("C:\Temp\IMG01.jpg", 271, "CARDA Industries Inc.")
' Call WIA_SetExifPropety("C:\Temp\IMG01.jpg", 272, "cPhone V6.0", "C:\Temp\IMG01-modified.jpg")
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2022-02-10 Initial Public Release
'---------------------------------------------------------------------------------------
Sub WIA_SetExifPropety(sInitialImage As String, lPropertyId As Long, vValue As Variant, Optional sDestiationImage As String)
On Error GoTo Error_Handler
#Const WIA_EarlyBind = False 'True => Early Binding / False => Late Binding
#If WIA_EarlyBind = True Then
'Early Binding req: Microsoft Windows Image Acquisition Library vX.X
Dim oIF As WIA.ImageFile
Dim oIP As WIA.ImageProcess
Set oIF = New WIA.ImageFile
Set oIP = New WIA.ImageProcess
#Else
Dim oIF As Object
Dim oIP As Object
Const StringImagePropertyType = 1002
Set oIF = CreateObject("WIA.ImageFile")
Set oIP = CreateObject("WIA.ImageProcess")
#End If
With oIP
.Filters.Add (.FilterInfos("Exif").FilterID)
.Filters(1).Properties("ID") = lPropertyId
.Filters(1).Properties("Type") = StringImagePropertyType
.Filters(1).Properties("Value") = vValue
End With
oIF.LoadFile sInitialImage
Set oIF = oIP.Apply(oIF)
If sDestiationImage <> "" Then 'New file specified
If Len(Dir(sDestiationImage)) > 0 Then Kill sDestiationImage
oIF.SaveFile sDestiationImage
Else 'Overwrite original if no Dest. file specified
Kill sInitialImage
oIF.SaveFile sInitialImage
End If
Error_Handler_Exit:
On Error Resume Next
If Not oIP Is Nothing Then Set oIP = Nothing
If Not oIF Is Nothing Then Set oIF = Nothing
Exit Sub
Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: WIA_SetExifPropety" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Sub
Examples Of Its Usage
If you simply want to change a property and retain the same file name then you can do:
Call WIA_SetExifPropety("C:\Temp\IMG01.jpg", 271, "CARDA Industries Inc.")
If, on the other hand you wish to change a property value but save the modification as a different file, then you’d do:
Call WIA_SetExifPropety("C:\Temp\IMG01.jpg", 272, "cPhone V6.0", "C:\Temp\IMG01-modified.jpg")
Code Features
This code
- can be used as both Early or Late Binding, the choice is yours and set by changing the value of the WIA_EarlyBind constant.
- is architecture/bitness independent (works in both 32 and 64-bit installations)
- is application independent (should work in any VBA applications – Access, Excel, Outlook, PowerPoint, Word, …)
A Few Resources on the Subject
