Resize image in Word with macro

Whenever I need to walk through a wizard I make screenshots of the steps. This comes in handy when someone else needs to do the same thing or just for logging purpose. The screenshots come in a Word document. To resize these images to minimize the number of pages I use this macro:

Sub ResizeImage()
'
' ResizeImage Macro
' Selected image(s) are resized to 5 cm in width
'
    Dim shape As InlineShape
    ' iterate all selected shapes
    For Each shape In Selection.InlineShapes
        ' remain aspect ratio
        shape.LockAspectRatio = msoTrue
        ' set with to 5 cm
        shape.Width = CentimetersToPoints(5)
    Next
End Sub

Every Image selected (CTRL+A) will be resized to 5cm with aspect ratio locked. I pinned the macro to the Word Quick Access Toolbar for easy access.

Requested code by Nano07 with al little help from Graham Mayor

Sub ResizeImage()
'
' ResizeImage Macro 2
' Selected image(s) are scaled to 100% and moved behind text in top left corner of the page
'
    Dim shape As InlineShape
    Dim shapeRange As shapeRange
    
    ' iterate all selected shapes
    For Each shape In Selection.InlineShapes
        ' remain aspect ratio
        shape.LockAspectRatio = msoTrue
        ' set with to 100 %
        shape.ScaleWidth = 100
        ' convert to shape to get a shaperange
        shape.ConvertToShape
        Set shapeRange = Selection.shapeRange
        ' position relative to the page
        shapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
        shapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionPage
        ' anchor to 0.0
        shapeRange.Top = 0
        shapeRange.Left = 0
        ' set to behind text
        shapeRange.WrapFormat.Type = wdWrapBehind
    Next
End Sub

About erictummers

My work as a recruited developer changes almost every month. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Tooling and tagged , , . Bookmark the permalink.

12 Responses to Resize image in Word with macro

  1. Nano07 says:

    Hey Eric,

    Thanks for the tip. I was wondering if this macro can be slightly modified to add the following: Align the image to the top left corner of the page.

    My situation is that I have 8.5×11″ images that I use as backgrounds in the header section. BUT when I insert them in Word, the are imported at 71% of the original size. So what I’m manually doing is 1) putting the image “Behind text”, then resizing to 100% and then setting absolute postion to 0,0cm (top left corner of the page).

    Can this macro be modified to do that?

    Thanks man!

  2. erictummers says:

    I’ve added the macro to the post.

  3. Jonathan says:

    Thanks so much for the macro! I know this is a super old post, but I was wondering if you could extend your original macro by having it also add a caption after each photo and have it increase numerically, ie Picture 1 [Add description], next photo would have Picture 2 [Add description] and so on. Or would it be simpler to insert a new paragraph after each photo? I had tried to add “Selection.InsertParagraphAfter” and “Selection.Move” right before the “Next” to no avail.
    Thank you!

  4. weirenloh says:

    Hi thanks for the code.

    I tried using it but found that the aspect ratio was not kept, i.e. adjustment only on the width. Would you be able to figure out why? I am using Words 2007. The object I have is an image and I just need to paste it into the page and resize them.

    Thanks!

    • erictummers says:

      I’m unable to reproduce your issue. It sounds like the LockAspectRatio setting is lost. Could you check that the LockAspectRatio is set to msoTrue?
      shape.LockAspectRatio = msoTrue

  5. 2016 mac says:

    The original (first) macro dosnt work in word 2016 mac – do you know anything about this issue? Dim shape As InlineShape and For each… lines are red in the editor and running issues: “Compilation Error; syntax error”

  6. 2016 mac says:

    Newer mind the previous – didt work when copy-pasted. Works rewritten into the editor.

  7. Sven says:

    Hello Eric

    I like your macro, but i was wondering if you could write it a bit differently, so the the picture-size gets changed depending on the current one.

    For example that if the Picture has size “X” and you run the Macro once, it will become 20% smaller to size “Y”. And then that if you run the Macro again, it shrinks from size “Y” again for 20% instead of becoming the 20% smaller version of “X” again or even the original Picture-Size

    My idea was to make two macros (including keyboard shortcuts), one to increase and one to decrease the size of a given picture by let’s say 10 percent or something so you could quickly change the size by pressing these shortcuts

    I’ve tried the “included” keyboard-shordcut, that everyone in every single forum ever seems to reccomend (shift + Arrow Keys) but in my Case it just deselects the pictures.

    Regards,

    Sven

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s