none
VBA sizing macro for Word using item from clipboard RRS feed

  • Question

  • I need your help.  I have no experience with this, so I hope that my request makes sense.

    I am trying to create a macro for sizing (height and width) images that I paste into a Word document. These are not saved images, they are images that are copied out of a separate program. I hit a "Copy" button in the program and then hit the "Paste" button in Word.

    The required height is 2.29 inches.

    The required width is 4.78 inches.

    I also want the image to be "in front of text" from the Text Wrapping function.

    Since I have hundreds of images (done one at a time), I don't know if I should select all of the images at once and run the macro or run it every time I paste the image.

    Friday, January 18, 2019 5:18 PM

All replies

  • If you copy the images to disk, you could use a macro like the one in this link:
    http://www.msofficeforums.com/word-vba/16772-4-digital-images-1-page.html#post47919

    The macro there creates a table with fixed row heights & column widths to hold the images, which will automatically be constrained to fit within the cell dimensions.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, January 18, 2019 9:06 PM
  • Thank you for the suggestion.  Unfortunately, I have to be able to move the images around because there are blank spaces needed for other things.  I also have to mix and match images on different pages.
    Friday, January 18, 2019 10:00 PM
  • For all existing images in your document, you could use a macro like:

    Sub ReformatAllImages()
    Application.ScreenUpdating = False
    Dim Shp As Shape
    With ActiveDocument
      Do While .InlineShapes.Count > 0
        .InlineShapes(1).ConvertToShape
      Loop
      For Each Shp In .Shapes
        With Shp
          .WrapFormat.Type = wdWrapFront
          .LockAspectRatio = True
          .Width = InchesToPoints(4.78)
        End With
      Next
    End With
    Application.ScreenUpdating = True
    End Sub

    For a selected range of images, you could use a macro like:

    Sub ReformatSelectedImages()
    Application.ScreenUpdating = False
    Dim Shp As Shape
    With Selection
      Do While .InlineShapes.Count > 0
        .InlineShapes(1).ConvertToShape
      Loop
      For Each Shp In .ShapeRange
        With Shp
          .WrapFormat.Type = wdWrapFront
          .LockAspectRatio = True
          .Width = InchesToPoints(4.78)
        End With
      Next
    End With
    Application.ScreenUpdating = True
    End Sub

    For a new image that you want to add to the document, you could use a macro like:

    Sub InsertImage()
    Dim Shp As Shape
    With Dialogs(wdDialogInsertPicture)
      If .Display = -1 Then
        If .Name <> "" Then
          Set Shp = ActiveDocument.Shapes.AddPicture(FileName:=.Name, _
            LinkToFile:=False, SaveWithDocument:=True, Anchor:=Selection.Range)
        End If
      End If
    End With
    With Shp
      .LockAspectRatio = True
      .Width = InchesToPoints(4.78)
      .WrapFormat.Type = wdWrapFront
    End With
    End Sub

    You'll note that neither macro explicitly sets the image height. Although that can be done, locking the aspect ratio ensures the image doesn't get distorted.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Sunday, January 20, 2019 6:55 AM
    Saturday, January 19, 2019 3:53 AM
  • Thank you, Paul.  This is perfect! - except for one thing.  I would like to click on the image and hit a Quick Access Toolbar button (which I have already added) and resize the individual selection.  (I used the ReformatImages() macro from above).

    I tried replacing "With ActiveDocument" (line 4) with "With Selection", but that didn't work.

    Sorry to be a bother, this is my first attempt at a macro.

    Sunday, January 20, 2019 12:25 AM
  • See my updated previous reply.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, January 20, 2019 6:56 AM
  • Works perfectly!  Thank you, Paul.
    Monday, January 21, 2019 11:55 PM