How to Use a Word VBA Macro to Resize Images and Keep Aspect Ratio
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.

- 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.
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.
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.
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.
In the VBA Editor, click on Insert in the top menu bar, and then select Module to create a blank script window.
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
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.

Create a Macro to Resize All Images in the Document
Loop through all shapes and inline shapes in the active document to resize every image simultaneously.
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. Open Your Document in WPS Writer: Launch WPS Office and open your text document containing the images you wish to resize.
- 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. 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.

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.




