h1

Word: Resize all images in a document to the same width

February 7, 2014

Note:

  1. This post is an adaptation of an article by Helen Bradley in Australian Personal Computer magazine (January 2014 issue, p99). Full credit and kudos should go to Helen and APC for this information — I only tweaked it and expanded upon it to suit my purposes, and added full steps and screen shots.
  2. You should be familiar with writing macros using the VBA code editor in Word. This post will not take you step-by-step through that process.
  3. Read the warning notice below.

BEWARE! This macro WILL resize ALL images in your Word document to the same width — that includes any cover art, logos, images on landscape pages, etc. This may NOT be what you want and you may decide that this macro isn’t for you. As with any such global change, test it on a COPY of your original document before using it on your original. You have been warned.

What you will do:

  1. Create a form that any user can complete when they run the macro that lets them specify the width (in centimetres) they want for all images and inline shapes.
  2. Add the code to the form.
  3. Test the macro on a COPY of an existing document.

Step 1: Create the form

  1. Open the Visual Basic editor in Word.
  2. Select Normal in the Project Explorer.
  3. Select Insert > UserForm from the menu.
  4. Add these elements to the form — TWO command buttons, ONE spin button, TWO labels, and ONE text box. You’ll arrange them later (Step 12).
  5. Open the properties of the spin control and set these values: Max: 160 (this will allow a maximum width of 16 cm; sorry — I don’t know how to set this for inches); Min: 20 (minimum width of 2 cm); Small change: 5 (increments of 0.5 cm); Value: 50. Adjust these values to suit your circumstances (e.g. if you have a 2-column document and only want small widths for the images, set the Max to 75 [i.e. 7.5 cm] or similar).
  6. Open the properties of the text box and set the Value to 5 (this means the default displayed in the text box will be 5 cm; change it to suit your circumstances). If you only want your users to use the spin control to set the value and don’t want to let them change it in the text box, set the Locked property to True.
  7. Open the properties of one of the labels, delete the text in the Caption field and add some explanatory text there — see the screen shot below for my example.
  8. Open the properties of the other label and change the Caption text to cm (for centimetres).
  9. Open the properties for CommandButton1 and change the Caption text to Resize Images.
  10. Open the properties for CommandButton2 and change the Caption text to Exit.
  11. Open the properties for UserForm1 and change the Caption to Resize all images.
  12. Arrange all elements on the form to follow familiar GUI principles (e.g. explanatory text at the top, ‘cm’ label to the right of the text box and spin control to the right of that, command buttons at the bottom of the form etc.).

reszie_all_01

Step 2: Add the code

Now that you have your form, you can add the code that sits behind the various elements.

Spin Control code:

Double-click the spin control and add this line between the Sub and End Sub lines: TextBox1.Value = SpinButton1.Value / 10

You should end up with this:

reszie_all_02

Exit button code:

Double-click the Exit button and add this line between the Sub and End Sub lines: Unload me

You should end up with this:

reszie_all_03

Resize Images button code:

Double-click the Resize Images button and add these lines between the Sub and End Sub lines — make sure you use the exact case and punctuation as listed here (Note: Some lines shown below will wrap in WordPress — check the screen shot to see where the lines break; I’ve left these lines in as text so you can copy/paste them):

Dim insertedPicture As InlineShape
Dim insertedShape As Shape
Dim imgMult As Single
imgMult = TextBox1.Value * 28.34

For Each insertedPicture In ActiveDocument.InlineShapes
insertedPicture.Select
insertedPictureHeight = insertedPictureHeight * imgMult / insertedPicture.Width
insertedPicture.Width = imgMult
Next

For Each insertedShape In ActiveDocument.Shapes
insertedShape.Select
insertedShape.Height = insertedShape.Height * imgMult / insertedShape.Width
insertedShape.Width = imgMult

Next

Unload Me

You should end up with this:

reszie_all_04

Notes:

  • I’m not sure what the multiplication factor of 28.34 means, but I suspect it’s something to do with converting the underlying measurement units (twips? points?) in Word to centimetres.
  • The aspect ratio of the image is kept; when the macro resizes the width, it keeps the proportions for the height.

Code to open the form:

Under the Normal > Modules folder in Project Explorer, add this code to NewMacros:

Sub resizeImages()

UserForm1.Show

End Sub

It should look like this:

reszie_all_05

Step 3: Test

As I mentioned at the beginning of this post, you’ll need to test this macro to see if it suits your purposes. If you have images in your document that SHOULDN’T be resized, then either consider not using this macro at all, or using it for everything, then manually sizing those few images back to their original dimensions.

  1. Run the resizeImages macro on a COPY of your document.
  2. Check ALL images in your document to see if any changed that you didn’t want to change (look for such things as logos, cover page art elements, images in landscape orientation that are now much narrower than you wanted, etc.).
  3. You can undo the changes the macro made by clicking the Undo button as many times as necessary to undo the changes to all images.

***************

Finally, I again wish to acknowledge Helen Bradley‘s work in creating the original version of this macro and APC for publishing it. Please give thanks to her, not me.

10 comments

  1. Excellent code. I Changed a few things around to work for inches. Basically just change the multiplier (1 centimeter = 28.34 PostScript Points) (1 inch = 72 PostScript Points)


  2. Reblogged this on Power to Build.


  3. debug.print CentimetersToPoints(1)
    28,34646


  4. Reblogged this on ictunit3rubansdiaryblog and commented:
    Really cool and useful.


  5. Thank you so much. Couldn’t be easier.


  6. This is fantastic! I have never done anything like this in Word, I first had to google how to open the visual basic editor (alt + F11). But still I was done after like 15 minutes and it works perfectly. Very well explained!


  7. Hello, here is a code to resize image when they are in a table (array). Keeping proportion doesn’t work in others example i found.

    Sub taille_images()

    ‘ Taille_images Macro


    Dim image As InlineShape

    Selection.WholeStory
    Selection.Fields.Update

    For Each image In ActiveDocument.InlineShapes
    redim_ratio = 5 / image.Width
    nouvImageHeight = image.Height * redim_ratio
    image.Width = CentimetersToPoints(5)
    image.Height = CentimetersToPoints(nouvImageHeight)

    Next

    End Sub


  8. Hi, thanks so much for this! Just wanted some advice on how I would go about making the code recognize the difference between portrait images and landscape images so they are resized proportionately? Basically I want a way to have the long side of all the images treated as the ‘width’. It treats portrait images in this way when they have been rotated from landscape, but not if they were inserted as portrait originally. Thanks for your help.


  9. Further to above, it might be better if there was a way to have a form come up for portrait and then another form for the landscape images so there was the option to have them different widths if wanted.


  10. Your method crops the pics for me instead of scaling them.
    I need to scale the WHOLE picture (various sizes) to a specific width & height.



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 )

Connecting to %s

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

%d bloggers like this: