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

S

SWT

I am having trouble changing the style of a field in the header/footer in a
document 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?
 

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