Field in header/footer wth two different styles

S

SWT

I know it is possible to have a field in the header/footer to have two
different styles, I'm just not sure how to do it via VBA. To be more
specific, I'm putting an IF field into the header/footer, and I only want it
to show on certain pages (ie. IF PAGE = NUMPAGES "True Text" "False Text"). I
have the field working perfectly with the autotext, but what I'm having
trouble with is getting the "True Text" to be a different style from the
"False Text". I am able to do it by manually selecting the field code in the
document and switching the style, but is there a way to do it in VBA?

Thanks for the help in advance.
 
G

Graham Mayor

Are you inserting the field into the header with vba? If not it would be
simpler just to format the True Text and False Text as you want them when
you enter the fields?

If you are using vba to insert the fields then create two character styles
formatted as you require them (here Style1 and Style2 and then:

Dim Text1 As String
Dim Text2 As String
Text1 = "TRUE TEXT"
Text2 = "FALSE TEXT"
With Selection
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
.TypeText Text:="IF"
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
.TypeText "PAGE"
.MoveRight Unit:=wdCharacter, Count:=2
.TypeText Text:=Chr(61)
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
.TypeText "NUMPAGES"
.MoveRight Unit:=wdCharacter, Count:=2
.TypeText Text:=Chr(34)
.Style = ActiveDocument.Styles("Style1")
.TypeText Text:=Text1
.TypeText Text:=Chr(34) & Chr(32) & Chr(34)
.Style = ActiveDocument.Styles("Style2")
.TypeText Text:=Text2 & Chr(34)
.Fields.Update
End With
ActiveWindow.View.ShowFieldCodes = False

..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top