Word VBA Macro to Crop, Resize, and Adjust Selected Pictures
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.

- 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.
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.
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.
Press Alt + F11 to open the Visual Basic for Applications editor in Word, and locate your existing macro module.
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`
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.
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.

Process Every Picture in the Document Using a Loop
Use a 'For Each' loop to iterate through all inline shapes in the document, applying the same sizing and cropping logic to every image simultaneously.
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. Open Your Document: Launch WPS Writer and open the document containing the images you wish to adjust.
- 2. Access the Developer Tab: Navigate to the Developer tab on the top ribbon and click on 'Macros'.
- 3. Insert the VBA Code: Paste your updated looping or selection-based VBA code into the WPS Macro Editor and save.
- 4. Execute the Script: Select your target picture (or leave unselected if using the loop method) and click 'Run' to instantly apply all adjustments.

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.




