更改Word文档页脚中的文本颜色

时间:2015-01-21 12:45:10

标签: vba ms-word footer word-vba word-2010

我使用VBA将一些文本插入到文档的页脚中,然后我希望更改文本的颜色。

我已尝试使用Selection.Range.Font.ColorIndex,但这似乎不起作用。我也试过手动设置颜色和录制宏,但VBA没有注意到颜色的变化。

有人知道我怎么能做到这一点吗?感谢。

'**
 ' Insert the required text into the footer of the document
 '
 ' @param required String footerText    The text to insert into the document footer
 ' @param Boolean insertDate            Whether or no to insert the date into the document footer
 '*
Sub DoFooterText(ByVal footerText As String, _
                 Optional ByVal insertDate As Boolean = True)

    Selection.GoTo What:=wdGoToPage, Count:=2                       ' Go to page 2 of the document (no footer on page 1)
    Selection.MoveRight Unit:=wdCharacter, Count:=1                 ' Move the cursor to the right of the document
    ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryFooter     ' Switch the view to the header
    Selection.TypeText text:=footerText                             ' Insert the footer text
    If insertDate = True Then                                       ' Insert the date into the footer
        Selection.TypeText text:=vbCrLf
        Selection.InsertDateTime _
            DateTimeFormat:="dd/MM/yyyy", _
            InsertAsField:=False, _
            InsertAsFullWidth:=False, _
            DateLanguage:=wdEnglishUK, _
            CalendarType:=wdCalendarWestern
    End If
    Selection.Range.Font.ColorIndex = wdGray50                      ' Set the colour of the footer text
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument      ' Switch the view back to the main document
    Selection.HomeKey Unit:=wdStory                                 ' Move the cursor back to the top of the document

End Sub

1 个答案:

答案 0 :(得分:1)

您需要先选择要更改颜色的文本。尝试添加下面的行,就在您设置文本颜色的行之前。

相当于按 SHIFT + Home

Selection.HomeKey Unit:=wdLine, Extend:=wdExtend

Selection.Range.Font.ColorIndex = wdGray50                      ' Set the colour of the footer text

修改

如果要选择页脚中的所有文本,从输入文本的位置到页脚的开头(相当于按 CTRL + SHIFT + Home ),改为使用Selection.HomeKey Unit:=wdStory, Extend:=wdExtend