VBA & Macro Problems

Word VBA Macro to Crop, Resize, and Adjust Selected Pictures

Aamir Naveed AkramAamir Naveed Akram Nov 3, 2026 872 views

Question details

The user wants to modify a Word VBA macro so that picture formatting adjustments apply only to a specific selected photo or to all photos iteratively, rather than just the first picture.

How to Crop, Resize, and Adjust Selected Pictures Using a Word VBA Macro
Product
Word
Device & OS
not provided
Scenario
Automating image processing tasks such as resizing, cropping, contrast, and brightness adjustments for photos imported into Word tables using VBA.
Observed behavior
The current VBA code hardcodes a reference to InlineShapes(1), causing the cropping and adjustment effects to apply exclusively to the first picture in the document regardless of which photo is selected.
Before you start

Ensure your images are inserted as 'In Line with Text' rather than floating shapes, and verify that the Developer tab is enabled to access the VBA editor.

Solution 1Recommended

Apply Macro Adjustments to the Selected Picture

Modify the macro to target only the currently selected inline shape, using a selection count check to avoid runtime errors when no picture is selected.

By replacing the static document index with a selection-based reference, your macro will dynamically interact with the image you have clicked on. Validating the selection first prevents the code from failing if the cursor is simply placed on text.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications editor in Word, and locate your existing macro module.

2
Add a Selection Verification Check

At the beginning of your macro, add code to check if an inline picture is selected: `If Selection.InlineShapes.Count = 0 Then MsgBox "Please select an inline picture." : Exit Sub`

3
Modify the Target Reference

Find the line of code referencing `ActiveDocument.InlineShapes(1)` and replace it with `Selection.InlineShapes(1)`. Update all cropping, resizing, and brightness parameters to point to this new reference.

4
Test the Macro

Return to your Word document, click on a specific photo with your mouse, and run the macro to confirm the adjustments apply only to the selected image.

Apply Macro Adjustments to the Selected Picture
Error Prevention: Adding the `Selection.InlineShapes.Count` check is critical for beginner VBA projects, as it gracefully handles cases where the user forgets to select an image before running the macro.
Efficient Document Automation

Automate Picture Formatting Easily with WPS Office

WPS Office provides robust support for executing VBA macros, allowing you to seamlessly run customized scripts that batch-process, crop, resize, and adjust pictures within your documents.

  1. 1. Open Your Document: Launch WPS Writer and open the document containing the images you wish to adjust.
  2. 2. Access the Developer Tab: Navigate to the Developer tab on the top ribbon and click on 'Macros'.
  3. 3. Insert the VBA Code: Paste your updated looping or selection-based VBA code into the WPS Macro Editor and save.
  4. 4. Execute the Script: Select your target picture (or leave unselected if using the loop method) and click 'Run' to instantly apply all adjustments.
Fully compatible with Microsoft Word VBA macros and syntaxLightweight program offering fast execution of automated scriptsIntuitive Developer tab for easy macro creation and editingMaintains perfect formatting compatibility with doc and docx files
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a runtime error when trying to run a macro on a selected picture?

A runtime error typically occurs if you attempt to process a selection that does not contain an inline shape. Ensure you have actively clicked on a picture and that your code includes `If Selection.InlineShapes.Count = 0 Then Exit Sub` to prevent the code from running on empty selections.

Will this VBA macro work on floating pictures (pictures with text wrapping)?

No. The provided macro code targets `InlineShapes`. If your pictures have text wrapping (such as 'Square' or 'Tight'), they belong to the `Shapes` collection instead. You would need to update the VBA references from InlineShapes to Shapes to process them.

How can I record a macro instead of writing VBA code manually?

You can use the 'Record Macro' feature found under the Developer tab. However, recorded macros for picture cropping often default to referencing specific shapes like `InlineShapes(1)`. You will still need to manually edit the recorded VBA code to use loops or selection targets for dynamic processing.