【问题标题】:Change the colour of text in the footer of a Word document更改 Word 文档页脚中文本的颜色
【发布时间】:2015-03-19 23:52:43
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: vba footer ms-word word-2010


    【解决方案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

    【讨论】:

    • 另外,您可能想在footerText和日期之间添加一个空格?
    • 好地方,我在输出日期之前不小心删除了Selection.TypeText text:=vbCrLf。至于你的答案,它正在工作,但不是footerText,现在是上面的一行。谢谢。
    • 啊哈,将wdLine 更改为wdStory 就可以了。感谢您的帮助。
    • 好的,太好了! (我只是假设他们在同一条线上)
    • 对,我已经更新了答案,包括您关于选择两条线的评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2010-11-18
    相关资源
    最近更新 更多