Removing Image Exif Properties Using WIA in VBA

So I was wrong in my last article, I wasn’t quite done with WIA yet, I had forgotten about deleting properties!

If you missed my previous post regarding WIA and working with image Exif metadata, here are a few links to get you up to speed:

Today, I will provide 2 procedures:

  • Delete a single Exif metadata property
  • Delete all the Exif metadata properties

This one took me a little longer to figure out as I could not find any examples or documentation that actually explained how to make it work. So this was a lot of trial and error. Especially for the WIA_RemoveAllExifPropeties, figuring out that you couldn’t delete the DocumentName property or the whole thing didn’t work.

Enough blah, blah, here’s the code.

Delete a Single Exif Metadata Property

Once again, the code here is pretty straightforward. We build a filter to get the property we want, then we Remove it and finally we apply it before saving.

'---------------------------------------------------------------------------------------
' Procedure : WIA_RemoveExifPropety
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Remove the specified Exif metadata property from an image file
' 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       : Exif property id to remove
' sDestiationImage  : Fully qualified path and filename if you wish to create a new image
'                       instead of overwriting the existing one
'
' Usage:
' ~~~~~~
' Call WIA_RemoveExifPropety("C:\Temp\IMG01.jpg", 271)
' Call WIA_RemoveExifPropety("C:\Temp\IMG01.jpg", 271, "C:\Temp\IMG01-modified.jpg")
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2022-02-10              Initial Public Release
'---------------------------------------------------------------------------------------
Sub WIA_RemoveExifPropety(sInitialImage As String, lPropertyId As Long, Optional sDestiationImage As String)
    On Error GoTo Error_Handler
    #Const WIA_EarlyBind = False    'True => Early Binding / False => Late Binding
    #If WIA_EarlyBind = True Then
        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("Remove") = True
    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_RemoveExifPropety" & 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 delete a the property and retain the original file name then you can do:

Call WIA_RemoveExifPropety("C:\Temp\IMG01.jpg", 271)

If, on the other hand you wish to remove a property, but save the modification as a different file, then you’d do:

Call WIA_RemoveExifPropety("C:\Temp\IMG01.jpg", 271, "C:\Temp\IMG01-modified.jpg")

Delete All The Exif Metadata Properties

In this scenario, we load the image, iterate over the existing properties and build a separate filter for each one, remove it and finally apply all the filters to the image before saving.

'---------------------------------------------------------------------------------------
' Procedure : WIA_RemoveAllExifPropeties
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Remove all the Exif metadata properties from an image file
' 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
' sDestiationImage  : Fully qualified path and filename if you wish to create a new image
'                       instead of overwriting the existing one
'
' Usage:
' ~~~~~~
' Call WIA_RemoveAllExifPropeties("C:\Temp\IMG01.jpg")
' Call WIA_RemoveAllExifPropeties("C:\Temp\IMG01.jpg", "C:\Temp\IMG01-modified.jpg")
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2022-02-10              Initial Public Release
'---------------------------------------------------------------------------------------
Sub WIA_RemoveAllExifPropeties(sInitialImage As String, Optional sDestiationImage As String)
    On Error GoTo Error_Handler
    #Const WIA_EarlyBind = False    'True => Early Binding / False => Late Binding
    #If WIA_EarlyBind = True Then
        Dim oIF               As WIA.ImageFile
        Dim oIP               As WIA.ImageProcess
        Dim prp               As WIA.Property

        Set oIF = New WIA.ImageFile
        Set oIP = New WIA.ImageProcess
    #Else
        Dim oIF               As Object
        Dim oIP               As Object
        Dim prp               As Object
        Const StringImagePropertyType = 1002

        Set oIF = CreateObject("WIA.ImageFile")
        Set oIP = CreateObject("WIA.ImageProcess")
    #End If
    Dim i                     As Long

    oIF.LoadFile sInitialImage
    For Each prp In oIF.Properties
        If prp.PropertyID <> 269 Then 'We have to leave the DocumentName property!
            i = i + 1    'Element counter
            With oIP
                .Filters.Add (.FilterInfos("Exif").FilterID)
                .Filters(i).Properties("ID") = prp.PropertyID
                .Filters(i).Properties("Remove") = True
            End With
        End If
    Next prp

    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_RemoveAllExifPropeties" & 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 delete all the properties and retain the original file name then you can do:

Call WIA_RemoveAllExifPropeties("C:\Temp\IMG01.jpg")

If, on the other hand you wish to remove all the properties, but save the modification as a different file, then you’d do:

Call WIA_RemoveAllExifPropeties("C:\Temp\IMG01.jpg", "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, …)

Okay, this time I truly am finished with WIA and Exif metadata!

A Few Resources on the Subject

2 responses on “Removing Image Exif Properties Using WIA in VBA

  1. Russel Gall

    Danial, I tried your Delete all the exif properties with nothing being changed in the image. I tried using the replacement and that didn’t work so I dropped it into a different library. The picture I used was create by an IPhone so I believe it was set up correctly. I did make one change to the program, I took of the first parameter, picture full name from “as string” to just the name. That way I could pass a variable to it that contained the folder and pic name.
    Anyway, I digress, I checked the picture in the new library and it ad the exact exif data the original one had.

    Thank you
    Russel Gall

    1. Daniel Pineault Post author

      Very interesting. I have been able to replicate this on my end. I will have to dig deeper to try and figure out the issue, but you can simply switch to one of the other approaches I have available on my site, GDI+, FreeImage, …