Others

How to Use a Word VBA Macro to Resize Images and Keep Aspect Ratio

Camila MilosovichCamila Milosovich Oct 28, 2026 868 views

Question details

The user needs to resize floating or inline pictures to a specified width using a Word VBA macro while ensuring the original aspect ratio is maintained.

How to Resize Images in Word Using a VBA Macro While Preserving Aspect Ratio
Product
Word
Device & OS
not provided
Scenario
Automating the process of scaling one or multiple images uniformly in a Word document without distorting their proportions.
Observed behavior
Images need to be resized via VBA to an exact width (e.g., 3 inches) while the height automatically adjusts to lock the original aspect ratio.
Before you start

Ensure that the Developer tab is enabled in your Word ribbon to access the Visual Basic for Applications (VBA) editor. It is recommended to test macros on a copy of your document first.

Solution 1Recommended

Use a VBA Macro to Resize a Selected Image

Run a specific macro to resize a single selected floating or inline picture to a fixed width while locking its aspect ratio.

This macro identifies whether the selected image is a floating shape or an inline shape, ensures the aspect ratio is locked, and scales the width to 3 inches.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications window. Alternatively, go to the Developer tab and click on Visual Basic.

2
Insert a New Module

In the VBA Editor, click on Insert in the top menu bar, and then select Module to create a blank script window.

3
Paste the Macro Code

Copy and paste the following code: Sub ResizePic() Select Case Selection.Type Case wdSelectionShape With Selection.ShapeRange If .Type = msoPicture Or .Type = msoLinkedPicture Then .LockAspectRatio = msoTrue .Width = Application.InchesToPoints(3) End If End With Case wdSelectionInlineShape With Selection.InlineShapes If .Count > 0 Then If .Item(1).Type = wdInlineShapeLinkedPicture Or .Item(1).Type = wdInlineShapePicture Then .Item(1).LockAspectRatio = msoTrue .Item(1).Width = Application.InchesToPoints(3) End If End If End With End Select End Sub

4
Run the Macro on an Image

Close the VBA editor and return to your Word document. Select the image you want to resize, press Alt + F8 to open the Macros dialog, select ResizePic, and click Run.

Use a VBA Macro to Resize a Selected Image
Adjusting the Image Width: You can change 'Application.InchesToPoints(3)' to another value, such as 'Application.CentimetersToPoints(5)', if you prefer using centimeters or need a different size.
Efficient Document Editing

Resize Images Seamlessly in WPS Writer

WPS Office offers intuitive image formatting tools to easily resize images while maintaining proportions, saving you time without needing to write complex VBA code.

  1. 1. Open Your Document in WPS Writer: Launch WPS Office and open your text document containing the images you wish to resize.
  2. 2. Select and Manually Resize: Click on the image. Click and drag the corner sizing handles; WPS Writer will automatically lock the aspect ratio to prevent distortion.
  3. 3. Set Exact Dimensions: For precise sizing, right-click the image and select 'More Layout Options'. Under the Size tab, check 'Lock aspect ratio' and enter your desired exact width or height.
Intuitive drag-and-drop image resizing with automatic aspect ratio lockingHighly compatible with Microsoft Word document formats (.docx, .doc)Built-in precise layout options for professional document designFree and lightweight Office suite for everyday tasks
microsoft office alternative - wps office

Frequently Asked Questions

Can I use centimeters instead of inches in the VBA code?

Yes. You can replace the command 'Application.InchesToPoints(3)' with 'Application.CentimetersToPoints(7.5)' to set the image width using the metric system.

Why does the macro ignore some of my pictures?

Word treats text-wrapping images (Shapes) and in-line-with-text images (InlineShapes) differently. If your macro only targets one collection, the other type will be ignored. Ensure your code targets both ActiveDocument.Shapes and ActiveDocument.InlineShapes.

How do I enable macros in my Word document?

To run VBA scripts, you may need to adjust your macro security settings. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings, and select 'Disable all macros with notification' so you can approve the macro when it runs.