Search code examples
vbams-wordborder

Mimic word borders and shading option "apply to:" (text) with vba on an inline shape


I got a macro that has a table with the first row being the name for that table and all the subsequent rows are print screens pasted. What the macro does is that when a new print screen is pasted the image is resized to a specific size and is added a border of 050pt.

I was asked that the border be applied to text: For example: enter image description here

What I did on the image was select the whole table with the little cross on the upper left hand corner, right click on the mouse, select "Borders & Shading" option and finally format my border like so: .5pt Box single line style. All of this is fine the problem is where the red box lies, how do I mimic with VBA the "Apply to: text" option. When you select that option what does it do? how can you even set the border to text when the image behaves like an inlineShape is there any way to do this?

Note: As you can see it's Office Word 2010

Edit:

Is there any way to do it without using Selection? The thing is that my sub works like this: For Each inlineShape in ActiveDocument.InlineShapes To get all shapes in the document or can I use that For Each to store all shapes in a variable and treat that variable as a Selection? I do it like this so that after a number of changes on the document, the sub fires and it checks every screen shot in the document so that it's consistent even if changes are made.


Solution

  • Mostly, thinking of Range in Word we think of text. However, InlineShapes are part of Range objects in document. Both of them- text and InlineShapes can have borders. My simple idea for you is to try this code after you select some text and/or InlineShape.

    Sub BorderingText_InLnShapes()
    
    With Selection.Range
        .Borders(wdBorderTop).LineStyle = wdLineStyleSingle
        .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
        .Borders(wdBorderRight).LineStyle = wdLineStyleSingle
        .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
    End With
    End Sub
    

    When working with macro recorder you could have similar macro which refers to Font object:

    Sub Alternative()
    'based on recorder
    With Selection.Font
        With .Borders(1)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        .Borders.Shadow = False
    End With
    End Sub
    

    The code above works a bit different with InlineShape but almost the same with text. I hope it's a bit helpful.