Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How alter word 2003 doc last page footer without altering other pages?

Status
Not open for further replies.

JSmytheeGL

Programmer
Nov 20, 2007
1
US
How can the last page footer of a Word 2003 document be programmatically altered without altering the footer of the other pages?

A word template document is used with a visual basic program to generate over 26,000 individual word documents using details from a file. The individual word documents can have between 2-30 pages. The footer of this word template document contains a two cell table. Cell 1 of this table must contain a different value for the first, middle, and last pages of the word document. For example, the first page footer table cell 1 must be 20. Last page footer table cell 1 must be 10. The footer table cell 1 of the middle pages must be 00. The cell 1 values increment for each document and are not the same for each document.

If a section break is manually or programmatically added before the footer of the last page, the first page either appears blank or it becomes section 1 with the rest of the pages becoming section 2 even though the goal was for the last page footer to become section 2. Inserting a page at the end of the document and then attempting the section break has the same results.

How can a section break be forced before the last page footer without impacting the other pages?
 
If a section break is manually or programmatically added before the footer of the last page,"

"How can a section break be forced before the last page footer without impacting the other pages? "

It is important to understand that these are incorrect perceptions of how Word handles footers.

Footers are child objects of Sections. So you can not add a Section break before "the footer". The footer is in a Section.

You do indeed have to have the last page in its own Section. As for: ""How can a section break be forced before the last page footer without impacting the other pages? "

It can't. However, you can make the impact be what you want. Thare are a couple of ways to go about this, but here is one possibility.
Code:
Sub LastPage()
Dim oHF As HeaderFooter
Dim r As Range

Selection.EndKey Unit:=wdStory
Set r = ActiveDocument.Bookmarks("\page").Range
With r
   .Collapse Direction:=wdCollapseStart
   .InsertBreak Type:=wdSectionBreakNextPage
End With
Selection.Sections(1).PageSetup _
      .DifferentFirstPageHeaderFooter = False
Set oHF = Selection.Sections(1) _
      .Footers(wdHeaderFooterPrimary)
With oHF
   .LinkToPrevious = False
   .Range.Text = "Last page"
End With
Set r = Nothing
Set oHF = Nothing
End Sub
This:

1. makes a Range object of the last page
2. collapses that Range
3. inserts a Section break
4. sets DifferentFirstPage to False
5. unlinks the new footer from the previous
6. then makes it whatever (in this case..."Last Page").

The assumption is that you do, indeed, have DifferentFirstPage as TRUE to start with. So say you have a 20 page document that starts off as:

FirstPage footer: "First Page"
Other pages: "Other Pages"

All in one Section, Section 1.

The code above will end up with:

Section 1:
FirstPage footer: "First Page"
pages 2 - 19 footer: "Other Pages"

Section 2:
page 20 footer: "Last Page"

It makes no difference how many pages there to start with. 4, 20, 120. If it starts as ONE Section (with DifferentFirstPage), it will make the last page its own section, unlink it, including having it NOT DifferentFirstPage, and put it its own footer.

The actual content of the footer is a separate issue, but hopefully this may help to show how you can actually different content for the last page.

faq219-2884

Gerry
My paintings and sculpture
 
JSmytheeGL

If you're creating a document from a template, then inserting the following field coding into the tmplate's footer will do the job. No vba needed:
Code:
{IF{PAGE}= 1 "first page text" {IF{PAGE}= {NUMPAGES} "last page text" "text for middle pages"}}
Note: the field braces indicated in the code (ie '{}') are created in pairs via Ctrl-F9.

If want to create the footer programatically, as indicated, then you can insert the field codes via vba. That will also avoid the need for section breaks - which can be problematic if you don't know which printer drivers might be active when the document is viewed.

Cheers

[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top