VBA – Open Image With Paint

Microsoft Paint Icon

So, since I’ve recently had a few posts regarding opening images in Microsoft Photo Viewer, MS Photos:

I thought I’d also demonstrate how easy it is to open an image in Paint as I know many people love to use Paint.

Launching Paint

You can open Paint very easily by doing something like:

'---------------------------------------------------------------------------------------
' Procedure : Paint_Launch
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Open Paint (start the application)
' 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:
' ~~~~~~~~~~~~~~~~
' lWindowStyle : Window Style (Normal, Maximized, Minimized, ...)
'
' Usage:
' ~~~~~~
' Call Paint_Launch
'
' Call Paint_Launch(vbMaximizedFocus)
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2023-04-17              Initial Release
'---------------------------------------------------------------------------------------
Sub Paint_Launch(Optional lWindowStyle As VbAppWinStyle = vbNormalFocus)
On Error GoTo Error_Handler

    Call Shell("mspaint.exe", lWindowStyle)
    
Error_Handler_Exit:
    On Error Resume Next
    Exit Sub
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Source: Paint_Launch" & 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

Opening an Image in Paint

Once again, Paint makes it very easy to open and load an image. You can do so by simply doing:

'---------------------------------------------------------------------------------------
' Procedure : Paint_OpenImage
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Open the specified image in Paint
' 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
' lWindowStyle  : Window Style (Normal, Maximized, Minimized, ...)
'
' Usage:
' ~~~~~~
' Call Paint_OpenImage("C:\Temp\TestImage.png")
'
' Call Paint_OpenImage("C:\Temp\TestImage.png", vbMaximizedFocus)
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2023-04-17              Initial Release
'---------------------------------------------------------------------------------------
Sub Paint_OpenImage(sImg As String, _
                    Optional lWindowStyle As VbAppWinStyle = vbNormalFocus)
On Error GoTo Error_Handler

    'Be a good idea to 1st validate the file exists!
    'Perhaps also check the file type/ext to ensure it is compatible with Paint
    '   bmp, dib, gif, heic, heif, hif, ico, jfif, jpe, jpeg, jpg, jpg_large, msp, png, tif, tiff, webp
    '   ***** When I tested heic did not work! *****
    
    Call Shell("mspaint.exe """ & sImg & """", lWindowStyle)
    
Error_Handler_Exit:
    On Error Resume Next
    Exit Sub
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Source: Paint_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

Notice the extra quotes surrounding the sImg to accommodate file paths with spaces in it.

Print an Image

Did you know you can use Paint to print an image? Well you can, and it is very simple! Here’s how it’s done:

'---------------------------------------------------------------------------------------
' Procedure : Paint_PrintImage
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Print the specified image, via Paint, to the default printer
' 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
' sPrinterName  : Optional: Name of the printer to print to
'                   if ommited - prints to the default printer
'                   if supplied - prints to the specified printer
'
' Usage:
' ~~~~~~
' Call Paint_PrintImage("C:\Temp\TestImage.png")
'
' Call Paint_PrintImage("C:\Temp\TestImage.png", "Microsoft Print To PDF")
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2023-04-17              Initial Release
'---------------------------------------------------------------------------------------
Sub Paint_PrintImage(sImg As String, Optional sPrinterName As String)
On Error GoTo Error_Handler

    'Be a good idea to 1st validate the file exists!
    'Perhaps also check the file type/ext to ensure it is compatible with Paint
    '   bmp, dib, gif, heic, heif, hif, ico, jfif, jpe, jpeg, jpg, jpg_large, msp, png, tif, tiff, webp
    '   When I tested heic did not work!
    If sPrinterName = "" Then
        'Print to the default printer
        Call Shell("mspaint.exe /p """ & sImg & """")
    Else
        'Print to the specified printer
        Call Shell("mspaint.exe /p """ & sImg & """ /pt """ & sPrinterName & """")
    End If
    
Error_Handler_Exit:
    On Error Resume Next
    Exit Sub
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Source: Paint_PrintImage" & 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