Help changing a style of a field in a header or footer

  • Thread starter Thread starter SWT
  • Start date Start date
S

SWT

I am having trouble changing the style of a field in the header/footer
without it affecting the style of the text that was already in the
header/footer.

When I insert the field before the existing text, the style changes
perfectly fine...

range = section.Headers(wdHeaderFooterPrimary).range
range.InsertBefore("IF PAGE = NUMPAGES ""Hello World" & vbcrlf & """)
range.SetRange(rangeEndBeforeInsert, range.End)
range.Fields.Add(range, wdFieldEmpty, , False)
range.SetRange(rangeEndBeforeInsert, range.End)
range.Style = word.ActiveDocument.Styles("Style")

However, when I insert the field at the end of the range, set the range's
boundaries to the field, and change the style, it also changes the style of
the text at the beginning of the header/footer...

range = section.Headers(wdHeaderFooterPrimary).range
range.InsertAfter("IF PAGE = NUMPAGES """ & vbcrlf & "Hello World""")
range.SetRange(rangeEndBeforeInsert, range.End)
range.Fields.Add(range, wdFieldEmpty, , False)
range.SetRange(rangeEndBeforeInsert, range.End)
range.Style = word.ActiveDocument.Styles("Style")

Again, the code works perfectly fine for the InsertBefore, it's just when I
insert the code after and I try to change the style, the text before the
field is changed to that style too. I wrote the code here in VB, but it's the
same with VB.net and VBA.

Any suggestions?
 
Back
Top