How to Resize All Word Document Images to Same Pixel Width via VBA
🔍 WiseChecker

How to Resize All Word Document Images to Same Pixel Width via VBA

Manually resizing every image in a long Word document to a uniform pixel width is tedious and error-prone. You might need all product screenshots, logos, or diagrams to match a specific width for a consistent report or proposal. Word does not include a built-in batch resize tool for images. This article explains how to use a Visual Basic for Applications macro to resize every picture in your document to the exact pixel width you specify.

Key Takeaways: Resizing All Images to a Uniform Width Using a VBA Macro

  • Alt + F11 (open VBA editor) then Insert > Module: Opens the environment where you paste and run the resize macro.
  • InlineShape.Width property: The VBA property that controls the width of images embedded inline with text.
  • Shape.Width property: The VBA property that controls the width of floating images not anchored to text.

ADVERTISEMENT

How the VBA Macro Resizes Images and What You Need Before Running It

The macro loops through every shape and inline shape in the active Word document. For each image it finds, it sets the Width property to a value you define in pixels. The macro does not change the height proportionally unless you add a separate line to lock the aspect ratio. By default, Word images maintain their original aspect ratio when you set only the width. The macro works on all picture types: JPEG, PNG, GIF, BMP, and TIFF. It does not affect charts, SmartArt, or embedded objects because those are not InlineShape or Shape picture types.

Before running the macro, close all other Word documents to avoid accidental changes. Save your document with a new name as a backup. The macro modifies the current document only. You need to enable macros in Word. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select Enable all macros. After you finish, revert this setting to Disable all macros with notification for security.

Steps to Create and Run the Image Resize Macro in Word

  1. Open the Visual Basic Editor
    Press Alt + F11 on your keyboard. The VBA editor window opens. If you see the Immediate pane at the bottom, you can ignore it.
  2. Insert a new module
    In the menu bar, click Insert > Module. A blank code window appears. This is where you paste the macro code.
  3. Paste the macro code
    Copy the following code and paste it into the module window:
    Sub ResizeAllImagesToWidth()
        Dim shp As Shape
        Dim inShp As InlineShape
        Dim targetWidth As Single
        
        targetWidth = InputBox("Enter the desired width in pixels:", "Image Width")
        If targetWidth <= 0 Then Exit Sub
        
        ' Resize floating shapes
        For Each shp In ActiveDocument.Shapes
            If shp.Type = msoPicture Then
                shp.LockAspectRatio = msoTrue
                shp.Width = targetWidth
            End If
        Next shp
        
        ' Resize inline shapes
        For Each inShp In ActiveDocument.InlineShapes
            If inShp.Type = wdInlineShapePicture Then
                inShp.LockAspectRatio = msoTrue
                inShp.Width = targetWidth
            End If
        Next inShp
        
        MsgBox "All images resized to " & targetWidth & " pixels wide."
    End Sub
    
  4. Run the macro
    Press F5 on your keyboard while the cursor is inside the macro code. A dialog box appears asking for the target width. Type a number such as 300 and click OK. The macro resizes every image in the document to that width.
  5. Save the document
    Press Ctrl + S to save your document. If you want the macro to be available in other documents, save the file as a macro-enabled document (.docm) or save the macro in the Normal.dotm template.

ADVERTISEMENT

If the Macro Does Not Work as Expected

The macro does not resize all images

Some images may be grouped. The macro only processes top-level shapes. To resize grouped images, ungroup them first or add a loop that checks for GroupItems. Also, the macro skips images inside headers, footers, and text boxes. To include those, you must add additional code to loop through HeaderFooter.Shapes and HeaderFooter.InlineShapes.

The macro changes the width but the height looks wrong

The macro locks the aspect ratio with LockAspectRatio = msoTrue. If the height still appears distorted, the image may be a linked picture or a placeholder that does not support aspect ratio locking. Convert linked images to embedded ones by breaking the link in File > Info > Edit Links to Files.

The macro gives an error "Run-time error '13': Type mismatch"

This occurs if you enter a non-numeric value in the input box. Close the error dialog, press F5 again, and type a whole number. If the error persists, check that your document contains at least one image. The macro runs safely on documents with zero images but will display a message that it completed.

VBA Resize Macro vs Manual Resize: Key Differences

Item VBA Macro Manual Resize
Time for 50 images Less than 2 seconds 5 to 10 minutes
Consistency All images set to exact same pixel width Possible slight variations from dragging
User skill required Basic familiarity with VBA editor No special skills
Document type support .docm files only (macro-enabled) Any Word document (.docx, .doc)
Undo capability One undo step after macro run Undo each individual resize

You can now resize every image in a Word document to the same pixel width in seconds using a simple VBA macro. Start by testing the macro on a copy of your document with a small width value like 200 pixels. For advanced control, modify the macro to also set a fixed height or to skip images that are already smaller than the target width. Save the macro in your Normal.dotm template to access it from any document.

ADVERTISEMENT