Reputation: 12057
I'm using VBA to insert some text in to the footer of a document, and I wish to then change the colour of the text.
I've tried using Selection.Range.Font.ColorIndex
but this doesn't seem to work. I've also tried manually settings the colour and recording a macro, but VBA doesn't pick up on the colour change.
Does anybody know how I can achieve this? Thanks.
'**
' 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
Upvotes: 0
Views: 1606
Reputation: 5385
You need to select the text where you want to change the color first. Try adding the line below, right before the line where you set the color of the text.
It's the equivalent of pressing SHIFT+Home:
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.Range.Font.ColorIndex = wdGray50 ' Set the colour of the footer text
EDIT:
If you want to select all of the text in the footer, from where the text is entered to the beginning of the footer (the equivalent of pressing CTRL+SHIFT+Home), use Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
instead.
Upvotes: 1