How to Write Word VBA Macro for Bulk Image Compression Before Save
🔍 WiseChecker

How to Write Word VBA Macro for Bulk Image Compression Before Save

When you insert multiple high-resolution images into a Word document, the file size can grow rapidly, making it difficult to share or store. Word does not offer a built-in one-click command to compress all images at once before saving. You can use a VBA macro to automate bulk image compression, reducing file size without manually adjusting each picture. This article explains how to write and run a VBA macro that compresses every image in your document before the save operation.

Key Takeaways: Writing a VBA Macro for Bulk Image Compression

  • Alt + F11 to open VBA editor: Access the Visual Basic for Applications environment where you write the macro.
  • ActiveDocument.InlineShapes or .Shapes: Use these objects to loop through all images in the document body and headers/footers.
  • PictureFormat.Compress method: The core command that reduces image resolution and removes cropped areas.
  • Application.Options.PictureCompression: Set this property to True before compression to ensure Word applies compression settings.

ADVERTISEMENT

How Word Stores Images and Why Compression Matters

Word stores each inserted image at its original resolution by default. A 10-megapixel photo from a digital camera can occupy 5 to 10 MB inside the document. When you insert twenty such images, the file size exceeds 100 MB. This makes the document slow to open, difficult to email, and problematic for cloud storage.

Word’s built-in compression feature reduces the resolution of all images to a target PPI, typically 220 PPI for web use or 150 PPI for email. It also discards cropped areas that are hidden from view. You can apply these settings manually via File > Options > Advanced > Image Size and Quality, or through the Compress Pictures button in the Picture Format ribbon tab. However, neither method allows you to compress images automatically before every save. A VBA macro solves this limitation by running the compression logic when you trigger it, either manually or automatically on save.

Prerequisites for Running a VBA Macro

Before writing the macro, ensure your Word security settings allow macros. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Select Enable all macros. This setting is temporary for development; revert to Disable all macros with notification after testing. You also need basic familiarity with the VBA editor. The macro described works in Word 2016, Word 2019, Word 2021, and Word for Microsoft 365.

Writing the VBA Macro for Bulk Image Compression

The macro loops through all inline images and floating shapes in the document body, then applies compression. It also supports images in headers and footers. The code uses the PictureFormat.Compress method which requires a PictureCompression object.

  1. Open the VBA Editor
    Press Alt + F11 in Word. The Visual Basic for Applications window opens. If the Project Explorer is not visible, press Ctrl + R to show it.
  2. Insert a New Module
    In the Project Explorer, locate Normal or your current document name. Right-click it and select Insert > Module. A blank code window appears.
  3. Paste the Macro Code
    Copy and paste the following code into the module window:
    Sub CompressAllImages()
        Dim shp As Shape
        Dim ils As InlineShape
        Dim s As Section
        Dim hf As HeaderFooter
    
        ' Set compression options
        Application.Options.PictureCompression = True
    
        ' Compress inline shapes in document body
        For Each ils In ActiveDocument.InlineShapes
            If ils.Type = wdInlineShapePicture Then
                ils.PictureFormat.Compress ppResolutionDefault, msoFalse
            End If
        Next ils
    
        ' Compress floating shapes in document body
        For Each shp In ActiveDocument.Shapes
            If shp.Type = msoPicture Then
                shp.PictureFormat.Compress ppResolutionDefault, msoFalse
            End If
        Next shp
    
        ' Compress images in headers and footers
        For Each s In ActiveDocument.Sections
            For Each hf In s.Headers
                For Each shp In hf.Shapes
                    If shp.Type = msoPicture Then
                        shp.PictureFormat.Compress ppResolutionDefault, msoFalse
                    End If
                Next shp
                For Each ils In hf.InlineShapes
                    If ils.Type = wdInlineShapePicture Then
                        ils.PictureFormat.Compress ppResolutionDefault, msoFalse
                    End If
                Next ils
            Next hf
            For Each hf In s.Footers
                For Each shp In hf.Shapes
                    If shp.Type = msoPicture Then
                        shp.PictureFormat.Compress ppResolutionDefault, msoFalse
                    End If
                Next shp
                For Each ils In hf.InlineShapes
                    If ils.Type = wdInlineShapePicture Then
                        ils.PictureFormat.Compress ppResolutionDefault, msoFalse
                    End If
                Next ils
            Next hf
        Next s
    
        MsgBox "All images compressed.", vbInformation
    End Sub
    
  4. Run the Macro
    Press F5 or click Run > Run Sub/UserForm. The macro compresses all images and shows a confirmation message. Alternatively, close the editor and press Alt + F8, select CompressAllImages, then click Run.
  5. Save the Document
    After running the macro, save the document normally (Ctrl + S). The file size should be significantly smaller.

Customizing the Compression Resolution

The macro uses ppResolutionDefault which applies the default resolution set in Word Options. To specify a specific PPI, replace ppResolutionDefault with one of the following constants:

  • ppResolution200 – 200 PPI, suitable for high-quality printing
  • ppResolution150 – 150 PPI, good for most documents
  • ppResolution96 – 96 PPI, best for email or screen viewing
  • ppResolution72 – 72 PPI, lowest quality but smallest file size

For example, to compress to 150 PPI, change the line to: ils.PictureFormat.Compress ppResolution150, msoFalse

Running the Macro Automatically Before Save

To compress images automatically every time you save, use the DocumentBeforeSave event. This requires placing code in the ThisDocument class module. Double-click ThisDocument in the Project Explorer and paste the following:

Private Sub DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
    Call CompressAllImages
End Sub

Now every time you save, the macro runs compression first. Note that this can slow down the save process on large documents.

ADVERTISEMENT

Common Issues With the Compression Macro

Macro Does Not Compress Images in Headers or Footers

The code above includes header and footer loops. If you still find images in headers not compressed, verify that the images are not linked. Linked pictures are not embedded and cannot be compressed. Convert linked images to embedded by selecting them, right-clicking, and choosing Change Picture > From File, then reinserting the image without linking.

Compression Causes Image Quality Loss

The macro uses the resolution constant you specify. If the document requires high-quality printing, use ppResolution200 or ppResolutionDefault with a high default setting. To change the default, go to File > Options > Advanced > Image Size and Quality and set the target output to 220 PPI or higher.

Macro Runs But No Compression Occurs

Check that the images are embedded, not linked. Also verify that Application.Options.PictureCompression is set to True. If this property is False, the Compress method has no effect. The macro sets it to True at the start, but if an error occurs before that line runs, compression may not apply.

Error: Method Compress of Object PictureFormat Failed

This error occurs when the image is corrupted or in an unsupported format. Insert the image again using a standard format like JPEG or PNG. Also ensure the picture is not in a content control that restricts programmatic access. Unlock the content control or remove it temporarily.

Manual Compression vs VBA Macro: Key Differences

Item Manual Compression VBA Macro Compression
Number of clicks per image 3 or more per image 1 macro run for all images
Time for 50 images 5–10 minutes Under 10 seconds
Headers and footers included Must navigate manually Automatically included
Custom resolution per image Possible per image Same resolution for all
Automation before save Not possible Possible with DocumentBeforeSave event

You can now write and use a VBA macro to compress all images in a Word document before saving. Start by testing the macro on a copy of your document. Adjust the resolution constant to match your quality needs. For advanced automation, attach the macro to the DocumentBeforeSave event so compression happens automatically every time you save. Remember to restore macro security settings after testing to prevent unauthorized macros from running.

ADVERTISEMENT