Insert text into a range without affecting fields

  • Thread starter Wes from Scottsdale
  • Start date
W

Wes from Scottsdale

I'm working on some VBA that will insert a document number into the footer.

When I add my text into the footer, all fields are converted to their text
equivalent and no longer track accurately. I've been setting the value of
ActiveDocument.Sections(#).Footers(#).Range.Text.

How can I modify the value of a range without affecting nearby fields?

Thanks.
 
J

Jay Freedman

Wes said:
I'm working on some VBA that will insert a document number into the
footer.

When I add my text into the footer, all fields are converted to their
text equivalent and no longer track accurately. I've been setting
the value of ActiveDocument.Sections(#).Footers(#).Range.Text.

How can I modify the value of a range without affecting nearby fields?

Thanks.

You can't do it that way, obviously. A variable of String data type, which
is what .Range.Text is, cannot contain fields.

You'll have to declare a Range object variable and set it to the particular
place in the footer -- between the fields, not including them -- at which
you want to insert the number.

To do that, your code will have to "know" what the footer looks like before
the insertion. If the number goes at the beginning or end of the footer, a
simple myRange.Collapse will do. If it has to go somewhere in the middle,
you can start with a bookmark at the point where you want the number and
point the range there, or you can parse the existing content of the footer
to find the right location.

If you show us the starting arrangement and the desired result, we can
suggest the best method.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
W

Wes from Scottsdale

Well, I figured out one way. I used .Range.InsertBefore/InsertAfter. That
adds my text without afffecting the field codes.

Thanks.
 

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