The purpose of the macro is to allow users to resize an image to a predefined width in Microsoft Word while maintaining its aspect ratio with convenience.
Macro Code
Sub ImageResize()
'
' ImageResize Macro
'
Dim w As Double
Dim h As Double
Dim ew As Double
Dim eh As Double
' Define the target width for the image
ew = 396
' Get the current width and height of the selected image
w = Selection.InlineShapes(1).Width
h = Selection.InlineShapes(1).Height
' Variable to store percentage for resizing
Dim percentage As Double
percentage = 1
' If the image width exceeds the target width, calculate the new height
If (w > ew) Then
percentage = ew / w
eh = h * percentage
End If
' Set the new width and height for the image
Selection.InlineShapes(1).Height = eh
Selection.InlineShapes(1).Width = ew
End Sub
Explanation:
-
ew: This variable holds the desired width for the image (396 units in this case).
-
The macro checks the current width (
w
) and height (h
) of the selected image. -
If the width of the image exceeds the target width (
ew
), it calculates the resizing percentage and adjusts the height (eh
) to maintain the aspect ratio. -
The image is then resized to the new dimensions.
How to Use:
-
Open Microsoft Word and press
Alt + F11
to open the VBA editor. -
In the editor, go to
Insert > Module
and paste the above macro code. -
Close the editor and run the macro whenever you want to resize an image inserted into the Word document.