Hi all,
Very interesting task. I like the code you posted and it's easily adaptable
to place the nested fields in the last section. But we don't know the end
user's Page setup (different first page, different odd/even pages). I
developed the following routine to incorporate the fields even if those
options haven't been selected. That is, you can run this routine and then
enable these options later, and the text will still appear.
Public Sub ModifyFooterOnLastPage()
Dim oSec As Section
With ActiveDocument
Set oSec = .Sections(.Sections.Count)
End With
With oSec
Set oRng = .Footers(wdHeaderFooterPrimary).Range
Call fInsertText(oRng:=oRng)
Set oRng = .Footers(wdHeaderFooterEvenPages).Range
Call fInsertText(oRng:=oRng)
Set oRng = .Footers(wdHeaderFooterFirstPage).Range
Call fInsertText(oRng:=oRng)
End With
End Sub
Public Sub fInsertText(oRng As Range)
Dim oRng1 As Range
Dim oRng2 As Range
Dim oRng3 As Range
With oRng
.Collapse Direction:=wdCollapseEnd
.Text = "IF PAGE = NUMPAGES ""Text here"""
Set oRng1 = .Duplicate
Set oRng2 = .Duplicate
Set oRng3 = .Duplicate
End With
Call fInsertFields(oRange:=oRng1, sText:="PAGE", bWild:=False)
Call fInsertFields(oRange:=oRng2, sText:="NUMPAGES", bWild:=False)
Call fInsertFields(oRange:=oRng3, sText:="IF*here""", bWild:=False)
End Sub
Public Sub fInsertFields(oRange As Range, sText As String, bWild As Boolean)
With oRange
Set oRng = .Duplicate
oRng.Find.Execute FindText:=sText, MatchCase:=True, MatchWildcards:=bWild
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, _
PreserveFormatting:=False
.Fields.Update
End With
End Sub
HTH,
Dave