VBA – WIA – Check If A File Is In An Acceptable Format

Over the years, I’ve released a number of posts and functions for working with images using WIA:

so on and so forth!

One important thing to do is to ensure we are passing the proper type of files to WIA to work with.  WIA cannot work with all image files and is limited to:

  • bmp
  • gif
  • jpeg/jpg
  • png
  • tif/tiff

As such, we have 2 choices to ensure we don’t run into issues should we pass the wrong file type:

  • Add error handling to each procedure to handle this type of exception
  • Create a function to perform this validation and use it prior to using WIA to working with the file(s)

Now my procedures always include a generic error handler and you can use error trapping to handle incompatible file types ( error -2147024809 The parameter is incorrect.  => Unrecognized format). but I thought I’d share a couple functions that could be used instead.

Validating The File’s Extension

The simplest solution would be to parse out the file’s extension and validate it against the known acceptable formats.  To do so we could create a simple function like:

'---------------------------------------------------------------------------------------
' Procedure : IsImageFile
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Validates whether or not the file is an image file that WIA can work with
'               by checking the file extension
' 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: None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sImage    : Fully qualified path and filename of the file to test/validate
'
' Usage:
' ~~~~~~
' IsImageFile("C:\Temp\Temp.jpg")
'   Returns -> True
' IsImageFile("C:\Temp\Temp.webp")
'   Returns -> False
' IsImageFile("C:\Temp\Book1.xlsx")
'   Returns -> False
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2023-04-12
'---------------------------------------------------------------------------------------
Public Function IsImageFile(sImage As String) As Boolean
    On Error GoTo Error_Handler
    Dim sFileExtension As String
    
    sFileExtension = Right(sImage, Len(sImage) - InStrRev(sImage, "."))
    Select Case sFileExtension
    Case "bmp", "gif", "jpeg", "jpg", "png", "tif", "tiff"
        IsImageFile = True
    End Select

Error_Handler_Exit:
    On Error Resume Next
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: IsImageFile" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

Use WIA To Validate  The File

Another approach, probably more reliable, would be to use WIA to perform the validation.  I say more reliable simply because I’ve had numerous cases where the files to be automated have the wrong file extensions and thus might erroneously pass/fail the above function, but would not using this approach.

'---------------------------------------------------------------------------------------
' Procedure : WIA_IsImageFile
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Validates whether or not the file is an image file that WIA can work with
' 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:
' ~~~~~~~~~~~~~~~~
' sImage    : Fully qualified path and filename of the file to test/validate
'
' Usage:
' ~~~~~~
' WIA_IsImageFile("C:\Temp\Temp.jpg")
'   Returns -> True
' WIA_IsImageFile("C:\Temp\Temp.webp")
'   Returns -> False
' WIA_IsImageFile("C:\Temp\Book1.xlsx")
'   Returns -> False
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2023-04-12
'---------------------------------------------------------------------------------------
Public Function WIA_IsImageFile(sImage As String) As Boolean
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

        Set oIF = New WIA.ImageFile
    #Else
        Dim oIF               As Object
        
        Set oIF = CreateObject("WIA.ImageFile")
    #End If

    Set oIF = CreateObject("WIA.ImageFile")
    oIF.LoadFile sImage
    WIA_IsImageFile = True

Error_Handler_Exit:
    On Error Resume Next
    If Not oIF Is Nothing Then Set oIF = Nothing
    Exit Function

Error_Handler:
    If Err.Number <> -2147024809 Then    '   The parameter is incorrect. / unrecognized format
        MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
               "Error Number: " & Err.Number & vbCrLf & _
               "Error Source: WIA_IsImageFile" & vbCrLf & _
               "Error Description: " & Err.Description & _
               Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
               , vbOKOnly + vbCritical, "An Error has Occurred!"
    End If
    Resume Error_Handler_Exit
End Function

If we don’t want to use error trapping we could modify the previous function to something like:

'---------------------------------------------------------------------------------------
' Procedure : WIA_IsImageFile
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Validates whether or not the file is an image file that WIA can work with
' 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:
' ~~~~~~~~~~~~~~~~
' sImage    : Fully qualified path and filename of the file to test/validate
'
' Usage:
' ~~~~~~
' WIA_IsImageFile("C:\Temp\Temp.jpg")
'   Returns -> True
' WIA_IsImageFile("C:\Temp\Temp.webp")
'   Returns -> False
' WIA_IsImageFile("C:\Temp\Book1.xlsx")
'   Returns -> False
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2023-04-12
'---------------------------------------------------------------------------------------
Public Function WIA_IsImageFile(sImage As String) As Boolean
    On Error Resume Next
    #Const WIA_EarlyBind = False    'True => Early Binding / False => Late Binding
    #If WIA_EarlyBind = True Then
        Dim oIF               As WIA.ImageFile

        Set oIF = New WIA.ImageFile
    #Else
        Dim oIF               As Object

        Set oIF = CreateObject("WIA.ImageFile")
    #End If

    Set oIF = CreateObject("WIA.ImageFile")
    oIF.LoadFile sImage
    On Error GoTo Error_Handler
    If Err.Number = 0 Then
        WIA_IsImageFile = True
        'Image was successfully loaded, Continue your code here
    'Else
        'Err.Clear
    End If

Error_Handler_Exit:
    On Error Resume Next
    If Not oIF Is Nothing Then Set oIF = Nothing
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: WIA_IsImageFile" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

and in reality, we could use this very same approach in any of my previous WIA functions found on the website. Only move forward with the code if no err was raised when loading the file as a WIA.FileImage.