使用 VBA 宏更改插入线颜色

Change insert line color with VBA macro

下面的代码是在 Microsoft Word 97-2003 中创建的(不是我创建的,并保存为 *.dotm),当时默认 "insert shape/line" 为黑色。用作具有特定封面、页眉、大纲样式等的程序模板。当*.doc 文件保存为*.docx 并激活“SignoffLine”宏时,插入行的颜色为蓝色(MS插入的 Word 2010 默认设置 Shape/Line?).

我可以更改每个文档的默认颜色,我可以通过 Normal.dotm 更改它,但是我想编辑下面的宏,所以插入的行总是黑色的。

Sub SignoffLine()
    On Error GoTo endthis
    i = Selection.Information(wdVerticalPositionRelativeToPage)
    Set oFFline = ActiveDocument.Shapes.AddLine(554, i + 12, 524, i + 12).Line

    With oFFline.Line
        .Weight = 0.75
    End With
        oFFline.Name = "hline" & idi
        idi = idi + 1
    endthis:
End Sub

很简单...您需要将 oFFline 对象定义为 Shape,然后设置其属性如下:

Sub SignoffLine()
    Dim oFFline As Shape
    Dim i As Integer

    On Error GoTo endthis

    i = Selection.Information(wdVerticalPositionRelativeToPage)
    Set oFFline = ActiveDocument.Shapes.AddLine(554, i + 12, 524, i + 12)

    With oFFline.Line
        .Weight = 0.75
        'set black color 
        .ForeColor.RGB = RGB(0, 0, 0)
    End With
    oFFline.Name = "hline" & idi
    idi = idi + 1

    endthis:
    Set oFFline = Nothing
End Sub

更多信息,请参阅:Shape Object (Word) and RGB