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

Working in a DevOps team is the best thing that happened to me. 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.

36 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

    • bfhaha says:

      Hello weirenloh.
      I had the same problem.
      I solved it by using
      shape.ScaleWidth = 80
      shape.ScaleHeight = 80
      instead of
      shape.LockAspectRatio = msoTrue
      shape.Width = CentimetersToPoints(5)

  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

  8. Sara says:

    Hi Eric,
    I’m hoping you can be of help. The macro provided works perfectly, thank you. But I have been desperately hunting for the code for a single macro that both inserts an image and resizes it. Do you know of a solution?
    Thank you,
    Sara

  9. Clauds says:

    Hi Eric,
    I’m hoping that you are still running this thread! I need to crop pictures to aspect ratio of Landscape 5:3, with width 4.5cm (and therefore height is 2.7cm). Are able to help at all? I am totally new to macros and am trying to record one, but I can’t work out to access the picture format tab as I can’t actually select the picture once recording has started.
    Thank you

  10. Toni says:

    Hello, I am trying to create a template for Biographies for our company. I created a mail merge document – easy peasy. HOWEVER. I need to insert a picture into EACH document (separate from the mail merge {unless it CAN be done via mail merge without modifying the report it’s being pulled from}). Before I merge the document – it’s no big deal to right click the picture and change it… but obviously that feature doesn’t work once the document is merged. I need to either 1. insert an image placeholder that is editable after the merge (right click and choose the correct photo file and be resized to the appropriate dimensions) or 2. pull the image file from a specified folder that matches the name on the bio (this I feel is impossible) or 3. something else that works??
    Any assistance is greatly appreciated. I am using MS Word 2016

  11. Luis Armas says:

    Hi, I have this code that assigns a image to a bookmark but I can’t figure out how to resize that image, can you help me?

    Selection.GoTo What:=wdGoToBookmark, Name:=”Score”
    Selection.InlineShapes.AddPicture FileName:=”D:\A+_Grade.png”, LinkToFile:=False, _
    SaveWithDocument:=True

  12. jeroeney says:

    dear
    can you also do it that it does all pictures in the document?

  13. jeroeny says:

    I have used this macro with succes for some time but now when i use it it doesnt scale the picture only the height gets adjusted.

  14. Kerry H says:

    Hi I am a total novice at macros. I have created a macro where I want to insert an image into a header (to make a letterhead) and tried to resize the image whilst recording. I now understand this is not possible but coding can be added to change the shape of the image but I don’t know how and I don’t know where. The image I want to be resized would be 3.57cm x 20cm. Please help!

    Here is the macro so far…..

    Sub Macro1()

    ‘ Macro1 Macro


    If ActiveWindow.View.SplitSpecial wdPaneNone Then
    ActiveWindow.Panes(2).Close
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
    ActivePane.View.Type = wdOutlineView Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.InlineShapes.AddPicture FileName:= _
    “W:\ADMIN\Dale Print 2021\MBC Header.jpg”, LinkToFile:=False, _
    SaveWithDocument:=True
    If ActiveWindow.View.SplitSpecial wdPaneNone Then
    ActiveWindow.Panes(2).Close
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
    ActivePane.View.Type = wdOutlineView Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    End Sub

  15. Kerry says:

    Hi Eric, I’m sorry for being thick but what part of my macro would you insert into the original?

  16. Kerry says:

    It’s not doing it. I recorded a macro where I opened up a header, brought in an image, resized it (which I know now you can’t do…) then closed the header but when I run the macro the image goes back to the smaller size when all I want to be able to do is to have the image inserted in the size it’s supposed to be. Anyway thank you for your help, I appreciate it

  17. Jay Pittard says:

    Eric, I’m a dummy when it comes to code. I haven’t written a line since my C++ course in 2001, and even then I was not among the top students in class.
    I have 124 pictures that are screenshots of webpages. It’s from a magazine which only publishes a digital edition. Reading online for long periods hurts my eyes, so I need to print the screenshots. I need to enlarge them from 33% (3.66″ high by 6.5″ wide) to 50% (5.65″ high by 10.05″ wide).
    My problem is that I don’t know where to enter the macro that you generously provided for us to copy and paste. Should I put it into the document, should I use the command prompt and enter the full document name (with extensions and all elements of the path) or somewhere else? Thank you very much for bearing with an archaic tech historian who is a compleat ID10T error with today’s tools.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.