在Word文档中的每个图像周围绘制边框

时间:2016-07-27 20:08:54

标签: image vba ms-word

有没有办法在Word中的每个图像周围添加边框?我知道我可以创建一个带边框的自定义段落样式并将图像放在那里,但也许我可以指定一个全局图像样式,就像在CSS中一样:

img { border: 1px solid #000 }

1 个答案:

答案 0 :(得分:0)

不幸的是,Word中没有可用的图片样式概念。因此,无法为类似于CSS的图像指定全局样式。

您可以做的是编写一个VBA宏,为所有图像添加边框。代码略有不同,具体取决于您的图片是否格式化为内嵌文字(InlineShape)或浮动(Shape):

Sub AddBorderToPictures()

    ' Add border to pictures that are "inline with text"
    Dim oInlineShape As inlineShape
    For Each oInlineShape In ActiveDocument.InlineShapes
        oInlineShape.Borders.Enable = True
        oInlineShape.Borders.OutsideColor = wdColorBlack
        oInlineShape.Borders.OutsideLineWidth = wdLineWidth100pt
        oInlineShape.Borders.OutsideLineStyle = wdLineStyleSingle
    Next

    ' Add border to pictures that are floating
    Dim oShape As shape
    For Each oShape In ActiveDocument.Shapes
        oShape.Line.ForeColor.RGB = RGB(0, 0, 0)
        oShape.Line.Weight = 1
        oShape.Line.DashStyle = msoLineSolid
    Next

End Sub

如果明显将线宽设置为wdLineWidth100pt是一个问题,您可以尝试使用实际的基础整数值,例如:

oInlineShape.Borders.OutsideLineWidth = 8

这是常量的定义方式:

public enum WdLineWidth
{
    wdLineWidth025pt = 2,
    wdLineWidth050pt = 4,
    wdLineWidth075pt = 6,
    wdLineWidth100pt = 8,
    wdLineWidth150pt = 12,
    wdLineWidth225pt = 18,
    wdLineWidth300pt = 24,
    wdLineWidth450pt = 36,
    wdLineWidth600pt = 48,
}