Lock Aspect Ratio via Macro RRS feed

  • Question

  • Hi there,

    im trying to create a Macro for Word in which i resize images that get inserted via { INCLUDEPICTURE }. These images are located in a table cell. My script so far looks like this:

    Sub resizeImages()
        Dim i As Long
        With ActiveDocument
            For i = 1 To .InlineShapes.Count
                With .InlineShapes(i)

                   If (checkVar("resizeImageHeight")) Then

                       If (checkVar("resizeImageWidth") = False) Then

                              .LockAspectRatio = msoTrue

                       End If

                       .Height = getVar("resizeImageHeight")
                   End If
                   If (checkVar("resizeImageWidth")) Then

                       If (checkVar("resizeImageHeight") = False) Then

                              .LockAspectRatio = msoTrue

                       End If

                       .Width = getVar("resizeImageWidth")
                   End If
               End With
           Next i
       End With
    End Sub

    My problem is, that Word doesnt care about the .LockAspectRatio Part. It just resizes the height and leaves the width as max size. According to another little Script ive written the images are inserted as InlineShape Object. But as InlineShapeObjects do have the .LockAspectRatiothis shouldnt be a problem at all id say.

    Is there any other reason why the images are getting stretched instead of properly resized?



    Wednesday, September 4, 2019 9:09 AM

All replies

  • For some reason Word honours the LockAspectRatio setting when you resize the picture using the User Interface, but not for this picture type when you change one of the dimensions via VBA.

    As far as I can tell this behaviour has been around for quite a long time on Windows Word. I assume it has to do with the fact that the way Word encodes pictures internally when you insert them via INCLUDEPICTURE is now very old-fashioned.

    Curiously, on Mac Word 365 the behaviour is as you might hope.

    There seem to me to be several ways you might be able to work around this, including
     a. maintain the aspect ratio manually in the VBA, i.e. change both the height and width
     b. use something like the following:

    Dim ishp As InlineShape
    Dim shp As Shape
    ' Using the first shape in the document as an example
    Set shp = ActiveDocument.InlineShapes(1).ConvertToShape
    shp.LockAspectRatio = msoTrue
    ' set the width to what you want while it's a shape
    shp.Width = 200
    ' convert it back
    Set ishp = shp.ConvertToInlineShape
    ' do other stuff with ishp…
    Set ishp = Nothing
    Set shp = Nothing
    The experiment I did here suggests that that leaves the field code intact, if that's what you need.

     c. if you know you need your images to fit into a certain space within the table, you may be able to fix the cell width and height in the table properties (i.e. select a cell, specify a fixed height for the row, a specific preferred width for the column, and uncheck Table Properties->Table->Automatically Resize to fit contents). But I think you will need to do that before you insert your INCLUDEPICTURE fields, and/or ensure that your INCLUDEPICTURE fields do not have a \*Mergeformat switch in them.

    Peter Jamieson

    Wednesday, September 4, 2019 12:05 PM