VBA – Open Image With Windows Photo Viewer

Ever had the need to specifically open an image in Windows Photo Viewer?

I’ve provided a multitude of ways to open files:

 

but what if we don’t necessarily want to use the default application and want to specifically open the image in the Windows Photo Viewer?


Well, my first thought was to search for something like “Windows Photo Viewer Command Line Switches”, but sadly this lead nowhere.

After some digging I found 2 techniques that worked for my needs:

'---------------------------------------------------------------------------------------
' Procedure : WPV_OpenImage
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Open an image in Window Photo Viewer
' 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:
' ~~~~~~~~~~~~~~~~
' sImg      : Fully qualified path and filename of the image to open
'
' Usage:
' ~~~~~~
' WPV_OpenImage "C:\Temp\Joe.jpg"
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2023-04-12
'---------------------------------------------------------------------------------------
Sub WPV_OpenImage(sImg As String)
On Error GoTo Error_Handler
    Dim sProgramFilesPath As String
    
    'sProgramFilesPath = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%ProgramFiles%") '32-bit machines
    sProgramFilesPath = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%ProgramW6432%") '64-bit machines
    Shell "RunDLL32 """ & sProgramFilesPath & "\Windows Photo Viewer\PhotoViewer.dll"", ImageView_Fullscreen " & sImg
    
Error_Handler_Exit:
    On Error Resume Next
    Exit Sub
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Source: WPV_OpenImage" & vbCrLf & _
           "Error Number: " & Err.Number & 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 Sub

and/or

'---------------------------------------------------------------------------------------
' Procedure : WPV_OpenImage2
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Open an image in Window Photo Viewer
' 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:
' ~~~~~~~~~~~~~~~~
' sImg      : Fully qualified path and filename of the image to open
'
' Usage:
' ~~~~~~
' WPV_OpenImage2 "C:\Temp\Joe.jpg"
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2023-04-12
'---------------------------------------------------------------------------------------
Sub WPV_OpenImage2(sImg As String)
On Error GoTo Error_Handler
    Dim sWindowsPath As String
    
    sWindowsPath = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%windir%")
    Shell "RunDLL32 " & sWindowsPath & "\System32\Shimgvw.dll, ImageView_Fullscreen " & sImg
    
Error_Handler_Exit:
    On Error Resume Next
    Exit Sub
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Source: WPV_OpenImage2" & vbCrLf & _
           "Error Number: " & Err.Number & 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 Sub

4 responses on “VBA – Open Image With Windows Photo Viewer