How to remove a border from an inline shape

回眸只為那壹抹淺笑 提交于 2020-07-09 14:39:08

问题


I am working in VBA on Word 2010.

I have some code to add borders to an inlineshape which is working ok, but I need to be able to remove the border and that doesn't seem to be working. I've searched through this site and can't find anything close apart from this:

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

Code is a follows:

Sub TestAddBorders()

Dim rngShape As InlineShape

For Each rngShape In ActiveDocument.InlineShapes
    With rngShape.Range.Borders
        .OutsideLineStyle = wdLineStyleSingle
        .OutsideColorIndex = wdPink
        .OutsideLineWidth = wdLineWidth300pt
    End With
Next rngShape

End Sub

Sub TestRemoveBorders()

Dim rngShape As InlineShape

For Each rngShape In ActiveDocument.InlineShapes
    With rngShape.Range.Borders
        .OutsideLineStyle = wdLineStyleNone
    End With
Next rngShape

End Sub

I am always left with a picture (inlineshape) that has a greyish border around it. Using "Picture Border > No Outline" on the Picture Tools > Format Tab removes it, but I can' find any way to do it in VBA. The wdLineStyleNone just doesn't seem to work and I can't see an option for colour = "none", or linewidth = "none"

Thank you.


回答1:


From MSDN:

To remove all the borders from an object, set the Enable property to False.

http://msdn.microsoft.com/en-us/library/office/ff196058.aspx

This will remove the borders as you applied them:

Sub TestRemoveBorders()

Dim rngShape As InlineShape

For Each rngShape In ActiveDocument.InlineShapes
    With rngShape.Range.Borders

        .Enable = False
    End With
Next rngShape
End Sub

The above method removes borders but not lines. To remove lines, try this:

With rngShape.Line
    .Visible = msoFalse
End With



回答2:


David's answer is correct, but I wanted to add to it for anyone who stumbles upon this later.

I prefer not to use the Borders method that I see most other people list to add a border to an InlineShape, and thanks to David's answer here I learned that you can just use the Line member like you can with normal Shapes!

I'm aware that this might not exactly answer the question for those of you who are not also setting the border yourself, but in my personal case it was helpful. With that in mind, here are the revised versions of methods to Add and Remove the borders from shapes.

Option Explicit

Sub PicturesAll_Borders_Show()

    'for pictures which are "In Line with Text"
    Dim inShp As InlineShape
    For Each inShp In ActiveDocument.InlineShapes
        If inShp.Type = wdInlineShapePicture Then
            With inShp.Line
                .Visible = True
                .Style = msoLineSingle
                .Weight = 1
                .ForeColor.RGB = RGB(0, 0, 0)
            End With
        End If
    Next inShp

    'for pictures which are "With Text Wrapping"
    Dim shp As Shape
    For Each shp In ActiveDocument.Shapes
        If shp.Type = msoPicture Then
            With shp.Line
                .Visible = True
                .Style = msoLineSingle
                .Weight = 1
                .ForeColor.RGB = RGB(0, 0, 0)
            End With
        End If
    Next shp

End Sub


Sub PicturesAll_Borders_Hide()

    'for pictures which are "In Line with Text"
    Dim inShp As InlineShape
    For Each inShp In ActiveDocument.InlineShapes
        If inShp.Type = wdInlineShapePicture Then inShp.Line.Visible = False
    Next inShp

    'for pictures which are "With Text Wrapping"
    Dim shp As Shape
    For Each shp In ActiveDocument.Shapes
        If shp.Type = msoPicture Then shp.Line.Visible = False
    Next shp

End Sub


来源:https://stackoverflow.com/questions/16588789/how-to-remove-a-border-from-an-inline-shape

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!