Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Scaling Graphics in a Macro.

Scaling Graphics in a Macro

by Allen Wyatt
(last updated October 24, 2016)

3

You may have a need to routinely scale graphics in your document by a certain percentage. Using the menus to do the scaling can get tiresome, so you may want to do the scaling by using a macro you can assign to a toolbar button or a shortcut key. The following macro will handle doing the scaling very nicely:

Sub PictSize()
    Dim PercentSize As Integer

    PercentSize = InputBox("Enter percent of full size", _
      "Resize Picture", 75)

    If Selection.InlineShapes.Count > 0 Then
        Selection.InlineShapes(1).ScaleHeight = PercentSize
        Selection.InlineShapes(1).ScaleWidth = PercentSize
    Else
        Selection.ShapeRange.ScaleHeight Factor:=(PercentSize / 100), _
          RelativeToOriginalSize:=msoCTrue
        Selection.ShapeRange.ScaleWidth Factor:=(PercentSize / 100), _
          RelativeToOriginalSize:=msoCTrue
    End If
End Sub

The macro first asks for a percentage by which you want to scale the selected image, offering 75 (75%) as the default. When you specify a percentage, the macro then checks to see if the selected graphic is an inline or a floating graphic. The reason for doing this is that the object specification is different in each case, as well as how the scaling is specified. Inline objects belong to the InlineShapes collection, while floating objects are set using the ShapeRange object.

If you want to resize all the graphics in your document by the same percentage, then you only need to modify the above macro so that it steps through each of the inline graphics and then each of the floating graphics.

Sub AllPictSize()
    Dim PercentSize As Integer
    Dim oIshp As InlineShape
    Dim oshp As Shape

    PercentSize = InputBox("Enter percent of full size", _
      "Resize Picture", 75)

    For Each oIshp In ActiveDocument.InlineShapes
        With oIshp
            .ScaleHeight = PercentSize
            .ScaleWidth = PercentSize
        End With
    Next oIshp

    For Each oshp In ActiveDocument.Shapes
        With oshp
            .ScaleHeight Factor:=(PercentSize / 100), _
              RelativeToOriginalSize:=msoCTrue
            .ScaleWidth Factor:=(PercentSize / 100), _
              RelativeToOriginalSize:=msoCTrue
        End With
    Next oshp
End Sub

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (7694) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Scaling Graphics in a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Understanding Page Sizes

When you create a document, you need to be concerned about the final size of the page you will be creating. Word supports a ...

Discover More

Extracting File Names from a Path

If you have a full path designation for the location of a file on your hard drive, you may want a way for Excel to pull just ...

Discover More

Inserting Text with a Macro

Need to have your macro insert a bit of text into your document? It's easy to do using the TypeText method.

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (menu)

Cropping Graphics

Need your hide some of the outside edges of a graphic? You can instruct Word to crop (or hide) those outside edges by ...

Discover More

Faster Picture Displaying

If you have a document with many graphics, you know that Word can sometimes be slow in displaying all those graphics. This ...

Discover More

Can't Select and Edit Graphics Elements

Adding graphics to your documents can make them livelier and easier to understand. What if you can't select and edit the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is seven minus 2?

2017-09-26 08:57:24

Neil

OK, more info on previous post. If I save the document between using the macros it works every time. So I can live with it now I guess, but it's clearly doing something that word doesn't like!


2017-09-26 08:08:12

Neil

Hi, I started by fixing it to 30% without the dialogue. It worked! So I went ahead and made 10 of them in a row, for changing the image size to 10%, 20% etc. all the way to 100%. Then I assigned the macros to keyboard shortcuts Alt+1, Alt+2 etc.

All appears to work, but half the time I get 'Run-time error '5223', Make a selection first'. It's as if word isn't recognising that the picture is selected, except that it is. If I hit end, then it works the next time, then doesn't work, then works... So something is toggling or the macro isn't leaving it in a state to run it again...? My knowledge of macros limited to trying to record one, and then searching to find out why it doesn't work, so assume I know nothing!

Opening the debugger highlights 'Selection.InlineShapes(1).ScaleHeight = PercentSize'

Macro code:

Sub Image_to_10pc()
'
' Image_to_10pc Macro
' Sets the image size to 10%
'
Dim PecentSize As Integer

PercentSize = 10

If Selection.InlineShapes.Count > 0 Then
Selection.InlineShapes(1).ScaleHeight = PercentSize
Selection.InlineShapes(1).ScaleWidth = PercentSize
Else
Selection.ShapeRange.ScaleHeight Factor:=(PercentSize / 100), _
RelativeToOriginalSize:=msoCTrue
Selection.ShapeRange.ScaleWidth Factor:=(PercentSize / 100), _
RelativeToOriginalSize:=msoCTrue
End If
End Sub

Sub Image_to_20pc()
'
' Image_to_20pc Macro
' Sets the image size to 20%
'
Dim PecentSize As Integer

PercentSize = 20

If Selection.InlineShapes.Count > 0 Then
Selection.InlineShapes(1).ScaleHeight = PercentSize
Selection.InlineShapes(1).ScaleWidth = PercentSize
Else
Selection.ShapeRange.ScaleHeight Factor:=(PercentSize / 100), _
RelativeToOriginalSize:=msoCTrue
Selection.ShapeRange.ScaleWidth Factor:=(PercentSize / 100), _
RelativeToOriginalSize:=msoCTrue
End If
End Sub

etc. all the way to 100. I tried giving them their own integers (percentsize10 etc.) but that made no difference.

Any help would be appreciated, thanks!

Oh, running Word 2016 on Win10 if that makes any difference.


2017-01-07 17:02:14

Lesliw WM

Worked perfectly for my Word 2016 document.

Thanks!


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.