adding text to header

B

Bert

I'm using VBA to alter chapter headers (one primary header, one even page
header) for each section (chapter) of a document.
(I know I could use Word's cross references and header styles to deal with
chapters,
but I'd prefer to do it this way.) I start with the insertion point in
the current section. When the macro runs, it:
1. finds the chapter number of the current section
2. adds a new section at the selection point (at the end of the currently
"active" chapter)
3. inserts the chapter number and chapter title in the body of the new
section
(thus moving the selection point to the new section),
4. and then adds a new chapter number at the appropriate point in the
headers.
The problem I'm having is that the macro doesn't seem to recognize there is
a new header in a new section.
It does not change the header in the new section, but rather changes the
header in the now-previous section.
It's almost as if it still thinks that previous section still is the
location of the selection point.
I'm using the following code to make the change in the header.
Selection.Sections(1).Headers(wdHeaderFooterPrimary).Range.InsertAfter
NxtChp
Any suggestions as to how to fix this?
Thanks.

Bert
 
J

Jezebel

Key to the solution is to work entirely with range objects: apart from using
the initial selection point as a reference, don't use or move the selection
at all.

It's not clear from your description whether you're always adding the new
section at the end of the document -- the whole exercise is much easier if
you are. Then doing the headers and footers will be simply --

With ActiveDocument.Sections(ActiveDocument.Sections.Count)
with .Headers(wdHeaderFooterPrimary)
.LinkToPrevious = FALSE
.Range = "......
end with
:
end with
 
B

Bert

Thanks.
At this point I only add to the end of the document, so yes, the
Sections.Count value would always equal the newly added section. (I'd like
the flexibility of actually inserting sections/chapters between existing
chapters, but that gets into renumbering and so on and at this point, I
gonna' keep it simple. Maybe someday...)
Thanks for your suggestions. I'll revise my macro!
 

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