Basically, if you are talking about Word, it is not really meaningful to talk about pages. There is no real "page" in Word.
Everything you see in Word goes through the printer-driver. Everything. What you see as a "page" in Word is what Word determines
would be a "page" if it was printed.
Thus, headers and footers do not belong to a page, they belong to the Section.
"For example, what you mention about not to use the Pages object but the Sections one. eeeeck, how do I decide which one to use is what takes me all day. "
There really is no which one. The key is truly thinking about
what you want to do.
Again, taking macropod's comment re: accessing the header...he is quite correct (and I have expanded on it describing using a second page) in that you do not need VBA at all to do what you want.
What You Want To Do:
IF a second page is created (assumably by adding content to Page1 so it spills over into page2),
THEN have that second page header contain "X" (whatever).
ANSWER: Set the Section to have DifferentFirstPage, and then create the second page, including the header containing "X" (whatever). Delete the second page. That information about the second page is persistent, that is, it is retained
even if there is no current second page.
That is macropod means by: "If this is a Word document, you could just setup its template's page layout with the 'different first page' option, and add the header/footer detail to the second page. You don't need vba for any of this. "
No VBA required.
However, if you DO need to use VBA to action a header (ANY header...or footer for that matter),
because it is an
object VBA can action instructions on it directly. It does not need to view anything.
As macropod put it:
Code:
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
.Range.Text = txtRecipient
badda bing badda boom
No View, no moving to the correct Section. It simply
makes the text of the Primary header of Section 1 = the value of txtRecipient. Immediately.
Headers and Footers (in Word) are for many people a very confusing issue. I admit they seem to be, but once you get a handle on what they are, and where they fit in the Object Model (and the somewhat odd way Microsoft treats them), they actually are straightforward.
For example, Microsoft has a
Document Model assumption - and Word has been like this from the start - that any new Section "should" have the same headers and footers as the previous Section. This is a conceptual assumption. One that I personally think is faulty.
The result of this assumption is that when you make a new section, all six headers and footers (and again ALL Sections
always have six header/footers) are the same as the previous section. LinkToPrevious=True is the
default.
This causes much grief for some. Again, I personally think it should be the other way around. A new Section headers and footers should be independent of the previous Section, unless you choose otherwise. However, that is not the way Word works. A new Section headers and footers are the same as the previous, unless you choose otherwise.
The reason I think this is...ummmm, incorrect, is
because of the Object Model.
Section 1:
DifferentFirstPage = True......blank, no text in header
DifferentOddEven = True......"Chapter X" on far left, "Title" on far right. This is for EVEN pages.
The ODD pages have....."Title" on far left, "Chapter X" on far right.
OK?
Now you make a new Section, but you do NOT have DifferentFirstPage or DifferentOddEven. It is going to have the same header for all the pages in the new Section.
Which one do you think it will be?
Chapter X Title
or
Title Chapter
I am just messin' with your head.
If you want to get a serious handle on things, it is worth your time to take a good look at the Word Object Model.
Or just keep posting here with questions. There are a number of people who know what they are doing, and are willing to help. I can not calculate how much I have learned from members like macropod, or Tony Jollans, or anotherhiggins, or.........
"A little piece of heaven
without that awkward dying part."
advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)
Gerry