Sunday, 5 November 2017

Last updated: 06/11/2017, 1 min read (without the code)

Photoshop VBA


Adobe Photoshop is probably the most famous raster graphics editor that is available on the market for almost three decades. You can do amazing things with this software. I have only played with it for a few hours, and I was quite surprised with the results. One of the things that intrigued me the most was the ability to program it. So, in this post I will take the opportunity to show you how to:

  • Resize an image.
  • Modify an image (apply filters).
  • Convert an image.

Here I will only scratch the surface on the topic of Photoshop VBA programming. However, I hope that it can give you some insights on what can be achieved.

VBA code

The code was written in early binding, just to allow the user to investigate the Photoshop's object model and the available options.

Option Explicit

Sub PlayingWithPhotoshop()

    'The macro demonstrates how to resize, modify (apply filters) and convert an image in Adobe Photoshop using VBA.
    'The macro requires the Adobe Photoshop library in order to work.
    'Go to Tools -> References -> Adobe Photoshop CSx Object Library, where x depends on your
    'Adobe Photoshop version (i.e. 6 or 5) you have installed on your PC.
    'Written By:    Christos Samaras
    'Date:          04/11/2017
    'Declaring the necessary variables.
    Dim InputImagePath      As String
    Dim OutputImagePath     As String
    Dim OutputWidthInPixels As Integer
    Dim PsApp               As Photoshop.Application
    Dim PsDoc               As Photoshop.Document
    Dim PsSaveOptions       As Photoshop.PNGSaveOptions
    'Set the necessary input variables.
    'The input image that will be modified. You can give the full path, e.g.:
    'InputImagePath = "C:\Users\Christos\Desktop\Input.jpg"
    InputImagePath = ThisWorkbook.Path & "\" & "Input.jpg"
    'The output image that will be created. Here it will be a png image. You can give the full path as well, e.g.:
    'OutputImagePath = "C:\Users\Christos\Desktop\Output.png"
    OutputImagePath = ThisWorkbook.Path & "\" & "Output.png"
    'The required width of the output image.
    OutputWidthInPixels = 750
    'Check if the input image path is valid.
    If FileExists(InputImagePath) = False Then
        MsgBox "The path of the input image is invalid!", vbCritical, "Input Image Path Error"
        Exit Sub
    End If
    'Create a new instance of Photoshop application and make it visible.
    On Error Resume Next
    Set PsApp = New Photoshop.Application
    If PsApp Is Nothing Then
        MsgBox "Sorry, it was impossible to start Photoshop!", vbCritical, "Photoshop Application Error"
        Exit Sub
    End If
    PsApp.Visible = True
    'Try to open the input image.
    Set PsDoc = PsApp.Open(InputImagePath)
    If PsDoc Is Nothing Then
        MsgBox "Sorry, it was impossible to open the input image!", vbCritical, "Image Opening Error"
        Exit Sub
    End If
    On Error GoTo 0
    'Print the image dimensions before resizing (in the Immediate window of the VBA editor).
    Debug.Print "Before: Width (px): " & PsDoc.Width * PsDoc.Resolution / 2.54 & " Height (px): " & PsDoc.Height * PsDoc.Resolution / 2.54
    'Resize the image.
    PsDoc.ResizeImage 2.54 * OutputWidthInPixels / PsDoc.Resolution
    'Print the image dimensions after resizing.
    Debug.Print "After: Width (px): " & PsDoc.Width * PsDoc.Resolution / 2.54 & " Height (px): " & PsDoc.Height * PsDoc.Resolution / 2.54
    'Apply the Sharpen filter.
    'Apply the Gaussian Blur filter within the specified radius (in pixels). Valid range: 0.1 - 250.0.
    PsDoc.ArtLayers(1).ApplyGaussianBlur 5
    'Create a new PNGSaveOptions object that will store the necessary saving parameters.
    Set PsSaveOptions = New Photoshop.PNGSaveOptions
    'Set the compression of the image. Valid range: 0 - 9, default: 0.
    PsSaveOptions.Compression = 5
    'Set the Interlaced option, which indicates whether the rows should interlace. Default value: false.
    PsSaveOptions.Interlaced = True
    'Save the modified image using the defined saving options.
    PsDoc.SaveAs OutputImagePath, PsSaveOptions, True, PsExtensionType.psLowercase 'PsExtensionType.psLowercase = 2 in late binding
    'Close the image.
    PsDoc.Close psDoNotSaveChanges 'psDoNotSaveChanges = 2 in late binding
    'Quit Photoshop.
    'Release the objects.
    Set PsSaveOptions = Nothing
    Set PsDoc = Nothing
    Set PsApp = Nothing
    'Inform the user about the process.
    If FileExists(OutputImagePath) = True Then
        MsgBox "The output image was successfully created!", vbInformation, "Finished"
        MsgBox "The output image was not created!", vbCritical, "Output Image Error"
    End If

End Sub

Function FileExists(FilePath As String) As Boolean
    'Checks if a file exists (using the Dir function).

    On Error Resume Next
    If Len(FilePath) > 0 Then
        If Not Dir(FilePath, vbDirectory) = vbNullString Then FileExists = True
    End If
    On Error GoTo 0
End Function


The picture below is the input image in this example:

Input Image - Before Processing

Input image: 4760 x 3120 pixels, jpg format. Click the image to view it in real size.

And here is the output image, after running the code:

Output Image - After Processing

Output image: 750 x 563 pixels, png format, with Sharpen and Gaussian Blur filter.

The result is neither fancy nor impressive (this wasn't my intent). However, it just shows some possible applications of VBA programming in respect to Photoshop.



The zip file contains a workbook with the VBA code and a sample image to play with it (the one that used in this example). The workbook can be opened with Excel 2007 or newer. Please enable macros before using it.

Did you like this post? If yes, then share it with your friends. Thank you!


Mechanical Engineer (Ph.D. cand.), M.Sc. Cranfield University, Dipl.-Ing. Aristotle University, Thessaloniki - Greece.
Communication: e-mail, Facebook, Twitter, Google+ and Linkedin. More info