Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Aug 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Using Macros to automatically paste, crop, resize, and reposition image to Word in one command

    Hello all,

    I am very new to the world of Microsoft Word Macros and am learning as I go, so please bear with me! I am in need of some help writing a Macro that does a combination of tasks. I had some success with parts of this but I have not been able to combine the different tasks into one Macro. I am hoping someone with the right skill or expertise may be able to post a solution. I am using Microsoft Office Professional Plus 2010 with Microsoft Word version 14. Here is what I a trying to do:

    I copy an image from a pdf using Control + C. From here I would like to write a Word macro that does the following:

    1) paste the image into Word
    2) crops the image (CropLeft = 0, CropTop = 0, CropRight = 250, CropBottom = 360)
    3) reduces the height (Height = 57)
    4) puts the image in front of the text (i.e., wdWrapFront)
    5) places one copy of the image at 0.16" to the right of margin and 1.15" below margin
    6) places a second copy of the image at 5.95" to the right of margin and 1.15" below margin

    The problems I have encountered are the following:
    A) I haven't figured out how to paste automatically. If I can add that to the script then great! Until now I have been pasting (Control + V) first then running the macro. It would be nice to do all in one click, if possible.
    B) I can't get #5 above to work, because I don't know how to write Macro script for image positioning. I have tried but failed a few times, which is why I am reaching out to the community.
    C) I can't get #6 to work, where I would be able to generate a second copy of the image (albeit repositioned).
    D) I can't seem to make a program that combines all code into one Macro. As you can see below from my script I have had to rely on two Macros up to this point. I would like to ideally run only one Macro that does this all.

    Can anyone help me with this programing who may know more than I about writing Macros in Word?

    Thanks in advance!




    Macro 1
    -----------------------------------------------
    Sub Macro1()
    Dim oILS As InlineShape
    Set oILS = Selection.InlineShapes(1)
    With oILS
    .PictureFormat.CropLeft = 0
    .PictureFormat.CropTop = 0
    .PictureFormat.CropRight = 250
    .PictureFormat.CropBottom = 360
    End With
    With oILS
    .LockAspectRatio = True
    .Height = 57
    End With
    lbl_Exit:
    Exit Sub
    End Sub


    Macro 2
    -------------------------------------------------

    Sub Macro2()
    Dim oShp As Shape
    With Selection.InlineShapes(1)
    Set oShp = .ConvertToShape
    End With
    oShp.WrapFormat.Type = wdWrapFront
    End Sub

  2. #2
    Star Lounger Graham Mayor's Avatar
    Join Date
    Mar 2016
    Location
    Cyprus
    Posts
    68
    Thanks
    0
    Thanked 24 Times in 24 Posts
    Maybe something like the following, however with your quoted measurements, I guess that the page is Landscape oriented, or the right hand image is not going to fit.
    Have you thought about using a table with two columns and fixed width cells? See http://www.gmayor.com/photo_gallery_template.html

    Code:
    Sub Macro1()
    Dim oILS As InlineShape, oILS2 As InlineShape
    Dim oShp As Shape
    Dim oRng As Range
        Set oILS = Selection.InlineShapes(1)
        With oILS
            Set oRng = oILS.Range
            oRng.Collapse 0
            oRng.FormattedText = oILS.Range
            Set oILS2 = oRng.InlineShapes(1)
            .PictureFormat.CropLeft = 0
            .PictureFormat.CropTop = 0
            .PictureFormat.CropRight = 250
            .PictureFormat.CropBottom = 360
            .LockAspectRatio = True
            .Height = 57
            Set oShp = .ConvertToShape
        End With
        With oShp
            .WrapFormat.Type = wdWrapFront
            .RelativeHorizontalPosition = _
            wdRelativeHorizontalPositionMargin
            .RelativeVerticalPosition = _
            wdRelativeVerticalPositionMargin
            .Left = InchesToPoints(0.16)
            .Top = InchesToPoints(1.15)
        End With
        With oILS2
            .PictureFormat.CropLeft = 0
            .PictureFormat.CropTop = 0
            .PictureFormat.CropRight = 250
            .PictureFormat.CropBottom = 360
            .LockAspectRatio = True
            .Height = 57
            Set oShp = .ConvertToShape
        End With
        With oShp
            .WrapFormat.Type = wdWrapFront
            .RelativeHorizontalPosition = _
            wdRelativeHorizontalPositionMargin
            .RelativeVerticalPosition = _
            wdRelativeVerticalPositionMargin
            .Left = InchesToPoints(5.95)
            .Top = InchesToPoints(1.15)
        End With
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - Word MVP
    http://www.gmayor.com

  3. #3
    New Lounger
    Join Date
    Aug 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This solution was awesome, and did the job very well! Thanks so much!
    The one thing that I would still wonder about is if script can be written to paste the image as part of the Macro (see #1 above). It worked perfectly once pasted in, but if that is possible to be included that would be the icing on the cake!

  4. #4
    Star Lounger Graham Mayor's Avatar
    Join Date
    Mar 2016
    Location
    Cyprus
    Posts
    68
    Thanks
    0
    Thanked 24 Times in 24 Posts
    Change the first part of the macro as follows

    Code:
    Sub Macro1()
    Dim oILS As InlineShape, oILS2 As InlineShape
    Dim oShp As Shape
    Dim oRng As Range
        Set oRng = Selection.Range
        oRng.Paste
        If Not oRng.InlineShapes.Count = 1 Then
            MsgBox "Copy the image to the clipboard first!"
            oRng.Delete
            GoTo lbl_Exit
        End If
        Set oILS = oRng.InlineShapes(1)
        With oILS   'etc
    Graham Mayor - Word MVP
    http://www.gmayor.com

  5. #5
    New Lounger
    Join Date
    Aug 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Graham Mayor you are brilliant! Thank you, it worked like a charm! Final solution code is:

    Sub Macro1()
    '
    ' Macro1 Macro

    Dim oILS As InlineShape, oILS2 As InlineShape
    Dim oShp As Shape
    Dim oRng As Range
    Set oRng = Selection.Range
    oRng.Paste
    If Not oRng.InlineShapes.Count = 1 Then
    MsgBox "Copy the image to the clipboard first!"
    oRng.Delete
    GoTo lbl_Exit
    End If
    Set oILS = oRng.InlineShapes(1)
    With oILS 'etc
    Set oRng = oILS.Range
    oRng.Collapse 0
    oRng.FormattedText = oILS.Range
    Set oILS2 = oRng.InlineShapes(1)
    .PictureFormat.CropLeft = 0
    .PictureFormat.CropTop = 0
    .PictureFormat.CropRight = 250
    .PictureFormat.CropBottom = 360
    .LockAspectRatio = True
    .Height = 57
    Set oShp = .ConvertToShape
    End With
    With oShp
    .WrapFormat.Type = wdWrapFront
    .RelativeHorizontalPosition = _
    wdRelativeHorizontalPositionMargin
    .RelativeVerticalPosition = _
    wdRelativeVerticalPositionMargin
    .Left = InchesToPoints(0.16)
    .Top = InchesToPoints(1.15)
    End With
    With oILS2
    .PictureFormat.CropLeft = 0
    .PictureFormat.CropTop = 0
    .PictureFormat.CropRight = 250
    .PictureFormat.CropBottom = 360
    .LockAspectRatio = True
    .Height = 57
    Set oShp = .ConvertToShape
    End With
    With oShp
    .WrapFormat.Type = wdWrapFront
    .RelativeHorizontalPosition = _
    wdRelativeHorizontalPositionMargin
    .RelativeVerticalPosition = _
    wdRelativeVerticalPositionMargin
    .Left = InchesToPoints(5.95)
    .Top = InchesToPoints(1.15)
    End With
    lbl_Exit:
    Exit Sub
    End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •