Your points about using the "with" clauses and 1, 2, and 3 as indexes for "Primary", "First", and "Even" are taken--for the purpose of my understanding, I have made all explicit
Ummmm, using With still makes them explicit. How could it not be explicit? You are most certainly NOT making them any more explicit by writing them out as separate instructions. Although, I think I understand what you are getting at.
However, you gain nothing by using separate instructions. You gain nothing by separating out the LinkToPrevious instructions from the Range.Text instructions. Nothing...at least code wise. In fact, in a wee technical way, you lose. The VBA parser must parse more instructions.
Hopefully, when you finish this up you will put the LinkToPrevious with all the other instructions PER headerfooter.
Code:
Dim oSection As Section
For Each oSection In ActiveDocument.Sections
For var = 1 To 3
oSection.Headers(var).LinkToPrevious = False
' your header text code
oSection.Footers(var).LinkToPrevious = False
' your footer text code
Next
Next
is much clearer to read - and write - than:
Code:
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).LinkToPrevious = False
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
ActiveDocument.Sections(1).Headers(wdHeaderFooterEvenPages).LinkToPrevious = False
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).LinkToPrevious = False
ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
ActiveDocument.Sections(1).Footers(wdHeaderFooterEvenPages).LinkToPrevious = False
Not only that, but the code above will only action Section 1
because it does it as separate instructions. The code using the variable will do it for ALL sections.
In your OP, you only mentioned a Section 1. Therefore I did not put anything in regarding LinkToPrevious.
However, it is good that you do add it. LinkToPrevious is the property that, by far, messes people up with headers and footers. If your document is going to have more than one Section, you must pay attention to LinkToPrevious. Good on you. I would say you understand pretty well. In particular, you have it bang on that you must do any LinkToPrevious = False
before you put content into headers or footers.
Just one big hint. Word moves the header/footer content
backwards through the document. It is part of the way Microsoft conceives a DOCUMENT.
Say you have (and for simplicity let's just do headers) a document with three Sections. All the LinkToPrevious are correct, and you have DifferentFirstPage and DifferentOddEven = True. The header text looks like this:
"Section 1 First Page" (#1)
"Section 1 Even Page" (#2)
"Section 1 Odd Page" (#3)
"Section 2 First Page" (#4)
"Section 2 Odd Page" (#5)
"Section 2 Even Page" (#6)
"Section 3 First Page" (#7)
"Section 3 Even Page" (#8)
"Section 3 Odd Page" (#9)
OK?
If you delete the Section breaks - NOT any pages, just the Section breaks - you end up with one Section. Section 1.
Right?
What will the text be?
It will be:
"Section 3 First Page" (#1)
"Section 3 Even Page" (#2)
"Section 3 Odd Page" (#3)
"Section 3 Even Page" (#4)
"Section 3 Odd Page" (#5)
"Section 3 Even Page" (#6)
"Section 3 Odd Page" (#7)
"Section 3 Even Page" (#8)
"Section 3 Odd Page" (#9)
Word will not, repeat NOT, retain what you put in as text in Section 1.
Let's do it again.
"Section 1 First Page" (#1)
"Section 1 Even Page" (#2)
"Section 1 Odd Page" (#3)
"Section 2 First Page" (#4)
"Section 2 Odd Page" (#5)
"Section 2 Even Page" (#6)
"Section 3 First Page" (#7)
"Section 3 Even Page" (#8)
"Section 3 Odd Page" (#9)
Now, this time, just remove the Section break for Section 3. Again, no changes to the pages, just the Section break. There are now two Sections - Section 1 and Section 2.
What will the text be? It will be:
"Section 1 First Page" (#1)
"Section 1 Even Page" (#2)
"Section 1 Odd Page" (#3)
"Section 3 First Page" (#4)
"Section 3 Odd Page" (#5)
"Section 3 Even Page" (#6)
"Section 3 Odd Page" (#7)
"Section 3 Even Page" (#8)
"Section 3 Odd Page" (#9)
Word moves header (and footer) content backwards through the document with deletions.
Once set, the LAST Section content will
always be the LAST Section content. No matter what the index number is.
In the first example (now only ONE Section, Section 1), the section headers content is the LAST Section content - ie. Section 3.
In the second example (two Sections, Section 1 and 2), the LAST Section still retains the LAST Section content - ie. Section 3
Even though Section 3 no longer exists. Do not get fooled into thinking header (or footer) text content is directly connected to the Section.
On the other hand, it is also important to note that ANY content, once set into a headerfooter, persists. If you:
DifferentFirstPage = True (checked in Page Setup)
Put "Section 1 First Page" in as text.
Then DifferentFirstPage = False (unchecked in Page Setup)
The text "Section2 First Page" persists. If you make DifferentFirstPage True again, that text will be there. So be careful if you use .Exists for headers or footers. Take the example above. DifferentFirstPage was set as True, text put into the header, then set as False. There is NO different first page. Run the following code.
Code:
Sub CheckIf()
If ActiveDocument.Sections(1).Headers(2).Exists = True Then
MsgBox "True"
Else
MsgBox "False"
End If
MsgBox ActiveDocument.Sections(1).Headers(2).Range.Text
End Sub
It checks to see if DifferentFirstPage "exists". DifferentFirstPage is currently NOT checked, so .Exists will return a "False" message.....then display the text of that supposed non-existing thing.
The .Exists property does NOT check the content of the header. It checks whether
Page Setup is True or False. NOT the header...because....
The headers and footers themselves (Primary, FirstPage, EvenPages)
always exist. You can not delete them.
Have fun.
faq219-2884
Gerry
My paintings and sculpture