VBA & Macro Problems

Resize Word Pictures with One VBA Macro and Modifier Keys

Rana GarciaRana Garcia Nov 18, 2026 869 views

Question details

Create one Word VBA macro that resizes selected pictures to predefined heights while preserving their aspect ratios.

Resize Word Pictures with One VBA Macro and Modifier Keys
Product
Microsoft Word
Device & OS
not provided
Scenario
The user wants to choose a picture height with Shift or Ctrl and handle both floating and inline pictures.
Observed behavior
A single macro should apply the chosen height without distorting the selected pictures.
Before you start

The modifier-key example below is for Word on Windows because it uses a Windows API. Open the document in desktop Word, enable the Developer tab if needed, and check that macros are permitted by your organization's security settings.

Solution 1Recommended

Use one Windows VBA macro with Shift and Ctrl

Choose a preset height at run time and resize selected floating shapes or inline pictures.

The macro uses 6.55 cm by default, 9.86 cm when Shift is held, and 12.5 cm when Ctrl is held. If both keys are held, Shift takes priority.

Paste this code into a standard module: #If VBA7 Then Private Declare PtrSafe Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer #Else Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer #End If Public Sub ResizeSelectedPictures() Dim heightCm As Double Dim selectedShapes As ShapeRange Dim shp As Shape Dim inlinePic As InlineShape Dim countResized As Long heightCm = 6.55 If GetAsyncKeyState(vbKeyShift) < 0 Then heightCm = 9.86 ElseIf GetAsyncKeyState(vbKeyControl) < 0 Then heightCm = 12.5 End If On Error Resume Next Set selectedShapes = Selection.ShapeRange On Error GoTo 0 If Not selectedShapes Is Nothing Then For Each shp In selectedShapes If shp.Type = msoPicture Or shp.Type = msoLinkedPicture Then shp.LockAspectRatio = msoTrue shp.Height = CentimetersToPoints(heightCm) countResized = countResized + 1 End If Next shp End If For Each inlinePic In Selection.Range.InlineShapes If inlinePic.Type = wdInlineShapePicture Or inlinePic.Type = wdInlineShapeLinkedPicture Then inlinePic.LockAspectRatio = msoTrue inlinePic.Height = CentimetersToPoints(heightCm) countResized = countResized + 1 End If Next inlinePic If countResized = 0 Then MsgBox "Select a picture or a range containing inline pictures first." End If End Sub

1
Open the VBA editor

In Word, press Alt+F11, then choose Insert > Module.

2
Add the macro

Paste the code into the module. If a procedure named ResizeSelectedPictures already exists in the project, rename one of the procedures so each macro has a unique name.

3
Add a run button

Go to File > Options > Quick Access Toolbar, choose Macros from the command list, and add ResizeSelectedPictures to the toolbar.

4
Select and resize pictures

Select a floating picture, or select a text range containing inline pictures. Click the macro button with no modifier for 6.55 cm, while holding Shift for 9.86 cm, or while holding Ctrl for 12.5 cm. Keep the modifier held until the macro starts.

5
Save the macro

Use File > Save As and select Word Macro-Enabled Document (.docm) if the macro is stored in the document.

Use one Windows VBA macro with Shift and Ctrl
Picture selection: The macro resizes picture and linked-picture objects only. Select a floating picture directly; for inline pictures, select a range that includes them.
Free Microsoft Office alternative

Edit and resize document pictures in WPS Writer

WPS Office is a free, lightweight Office alternative with a familiar interface and support for Word documents, making it easy to move everyday document editing to WPS Writer. It does not fix or guarantee compatibility with this Windows-specific Word VBA macro.

Resize pictures using Writer's picture formatting controls.Open and edit Microsoft Word formats such as DOC and DOCX.Use a familiar interface for everyday document editing.Work with a free, lightweight Office suite.
QA img-9

Frequently Asked Questions

How do I choose a different picture height?

In the macro, change the heightCm values assigned for the default, Shift, and Ctrl cases. The values are in centimeters and are converted to Word points before resizing.

Why does the macro say no pictures were selected?

Select a floating picture directly, or select a text range that includes inline pictures, then run the macro again. The example ignores objects that are not picture or linked-picture types.

Can I use this modifier-key macro in Word for Mac?

Not as written. The GetAsyncKeyState declaration calls the Windows user32 library. On a Mac, use separate preset macros and a shared resizing procedure instead.

Why is my macro missing after I add another one?

In Developer > Visual Basic, check that public macros have distinct names and are stored in a standard module. Save a document containing macros as a Word Macro-Enabled Document (.docm).