none
Would like VBA in Word to style an image in specific ways. RRS feed

  • Question

  • So, when I create a macro and go to style an image, I can't access those functions. Therefore, they don't end up writing to the VBA so I can make my "junior" attempts at some VBA.

    I'm trying to have the VBA work on the selected content of the document, determine if the selections are images and if so, perform the following:

    1. Center the image (this one should be easy)
    2. Size the image by constraining the proportions on a specified width.
    3. Put a border around the image with a specific RGB color value and specific border line thickness.

    Can anyone help?

    Many thanks in advance.


    -- Tony

    Wednesday, October 31, 2012 5:02 PM

Answers

  • Somthing like this:

    Sub ScratchMacro()
    'A quick macro scratch pad created by Greg Maxey
    '1.Center the image (this one should be easy)
    '2.Size the image by constraining the proportions on a specified width.
    '3.Put a border around the image with a specific RGB color value and specific border line thickness.
    Dim oILS As InlineShape
    If Selection.InlineShapes.Count = 1 Then
      Set oILS = Selection.InlineShapes(1)
      With oILS
        .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
        .Width = 50
        .LockAspectRatio = msoTrue
        With .Borders
          .OutsideLineStyle = wdLineStyleSingle
          .OutsideLineWidth = wdLineWidth150pt
          .OutsideColorIndex = RGB(15, 35, 245)
        End With
      End With
    End If
    End Sub

    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Wednesday, October 31, 2012 8:44 PM

All replies

  • Somthing like this:

    Sub ScratchMacro()
    'A quick macro scratch pad created by Greg Maxey
    '1.Center the image (this one should be easy)
    '2.Size the image by constraining the proportions on a specified width.
    '3.Put a border around the image with a specific RGB color value and specific border line thickness.
    Dim oILS As InlineShape
    If Selection.InlineShapes.Count = 1 Then
      Set oILS = Selection.InlineShapes(1)
      With oILS
        .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
        .Width = 50
        .LockAspectRatio = msoTrue
        With .Borders
          .OutsideLineStyle = wdLineStyleSingle
          .OutsideLineWidth = wdLineWidth150pt
          .OutsideColorIndex = RGB(15, 35, 245)
        End With
      End With
    End If
    End Sub

    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Wednesday, October 31, 2012 8:44 PM
  • Greg, thanks so much. This works GREAT!

    Do you know if I could call a specific style I've designated for image alignment and spacing instead of using the line...

    .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

    to use something like this instead?

    .Range.Style = Image

    Best,


    -- Tony

    Wednesday, October 31, 2012 10:26 PM
  • That should work if you have created and defined a style named "Image".


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Thursday, November 1, 2012 12:00 AM
  • I'm giving it a try and I get a Run-time error '5843':
    One of the values passed to this method or property is out of range.

    Am I calling the Style incorrectly in the following line?

    .Range.Style = Image?

    I do have a style called Image.
    Thanks so much Greg!

    Best,


    -- Tony

    Thursday, November 1, 2012 12:44 AM
  • .Range.Style = "Image"

    I only tested with a new "Style1" based on "Normal" and set the alingment to centered.  if the line above doesn't work, then I can only guess that there is something in the style definition that is conflicting with the image properties.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Thursday, November 1, 2012 1:18 AM
  • Couldn't seem to get the style to show up correctly. So, I just ditched it and used the following lines instead to achieve the same effect:

        .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
        .Range.ParagraphFormat.SpaceBefore = 12
        .Range.ParagraphFormat.SpaceAfter = 12

    While I'd rather call a style, I'm still just doing those three lines.

    However, when I run the code on an image, I get a black border no matter what RGB values I put in this line:

          .OutsideColorIndex = RGB(139, 195, 52)

    It should show a specific light green color. Instead, it always shows a black border.


    -- Tony

    Thursday, November 1, 2012 8:09 PM
  • Greg, man, I just don't get it. I've toiled away studying the code and researched in the MSDN library, but I can't seem to figure out why the following two lines (in bold) seem to have no affect on selected images?

    I even tried putting the code into a new macro inside a new document and the two lines still have no affect on images.

    Sub ResizeLargeImages()
    'A quick macro scratch pad created by Greg Maxey
    '1.Center the image (this one should be easy)
    '2.Size the image by constraining the proportions on a specified width.
    '3.Put a border around the image with a specific RGB color value and specific border line thickness.
    Dim oILS As InlineShape
    If Selection.InlineShapes.Count = 1 Then
      Set oILS = Selection.InlineShapes(1)
      With oILS
        .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
        .Range.ParagraphFormat.SpaceBefore = 12
        .Range.ParagraphFormat.SpaceAfter = 12
        .Width = 400
        .LockAspectRatio = msoTrue
        With .Borders
          .OutsideLineStyle = wdLineStyleSolid
          .OutsideLineWidth = wdLineWidth150pt
          .OutsideColorIndex = RGB(139, 195, 52)
        End With
      End With
    End If
    End Sub

    Any clues or where I can further research are greatly appreciated. Thanks for all the help. :)

    Best,
    Tony


    -- Tony

    Saturday, November 3, 2012 1:12 PM