How to Use a Field to Print Text in a Footer on Last Page

  • Thread starter Michael Stauffer
  • Start date
M

Michael Stauffer

I am trying to find a way to print the value of the IdentifierReference
Property in a document on the last page of the last section of a document
only.

I have referred to KnowledgeBase Article 211755
(http://support.microsoft.com/?kbid=211755), which shows how to setup a field
where text would display on every page except the last page of a document.
I’ve have adapted that code so that it correctly displays the field value on
the last page of a document with only one section, but the code does not work
if I try to apply it to a multi-section document.

I have created a custom document property named SectionCount and applied the
value of ActiveDocument.Sections.Count to it. I then applied an And operator
so that the field is now displaying the field value where {Current Page
Number} = {Number of Pages in the Current Section} AND {Current Section
Number} = {Number of Sections in the Document}

Here is a copy of my code at this point:

Sub InsertIdentifierReferenceFieldCode()
On Error GoTo Err_Handler

Dim fld As Field
Dim strFieldCode As String
Dim intSectionCounter As Integer, intCounter As Integer
Dim sec As Section

' Are any documents open?:
If Documents.Count = 0 Then Exit Sub

' Go to Page View:
If ActiveWindow.ActivePane.View.Type = wdNormalView Or _
ActiveWindow.ActivePane.View.Type = wdOutlineView Or _
ActiveWindow.ActivePane.View.Type = wdMasterView Then
ActiveWindow.ActivePane.View.Type = wdPageView
End If

' Go to Page 1:
Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst

' Go to the Footer:
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter

strFieldCode = " DOCPROPERTY " & Chr(34) & "Identifier1" & Chr(34) & " "


' Cycle through all sections of the document:
intSectionCounter = ActiveDocument.Sections.Count
intCounter = 1
Do Until intCounter = intSectionCounter + 1

' Work with the First Page Header of this section of the document:
Selection.HomeKey Unit:=wdLine
Selection.MoveEnd Unit:=wdStory
ActiveDocument.Sections(intCounter).Footers
wdHeaderFooterFirstPage).LinkToPrevious = False

' If the document already contains an Identifier1 field then delete
that field:
For Each fld In Selection.HeaderFooter.Range.Fields
If InStr(1, fld.Code, strFieldCode, vbTextCompare) Then
fld.Delete
End If
Next

' Add the Identifier1 field:
ActiveWindow.View.ShowFieldCodes = True

' Check to see if the CustomDocumentProperty named SectionCount
already exists:
Dim TestVal
TestVal =
ActiveDocument.CustomDocumentProperties("SectionsCount").Value

' Set the value of the CustomDocumentProperty named SectionCount:
ActiveDocument.CustomDocumentProperties("SectionsCount").Value =
ActiveDocument.Sections.Count

' Add the Identifier1 field:
With Selection
.HomeKey Unit:=wdStory
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:="IF ", PreserveFormatting:=True
.MoveRight Unit:=wdCharacter, Count:=5
.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
.TypeText Text:=" = "
.Fields.Add Range:=Selection.Range, Type:=wdFieldSectionPages
.TypeText Text:=" AND "
.Fields.Add Range:=Selection.Range, Type:=wdFieldSection
.TypeText Text:=" = "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"DOCPROPERTY ""SectionsCount"" ", PreserveFormatting:=True
.TypeText Text:=" "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"DOCPROPERTY ""Identifier1"" ", PreserveFormatting:=True
Selection.HeaderFooter.Range.Fields.Update
End With


' Work with the Primary Header of this section of the document:
Dim secTemp As Section
Set secTemp = ActiveDocument.Sections(intCounter)
If secTemp.PageSetup.DifferentFirstPageHeaderFooter = True And
ActiveDocument.BuiltInDocumentProperties("Number of Pages") > 1 Then
' Go to the "Second Page Header":
ActiveDocument.ActiveWindow.View.NextHeaderFooter
Selection.HomeKey Unit:=wdLine
Selection.MoveEnd Unit:=wdStory

ActiveDocument.Sections(intCounter).Footers(wdHeaderFooterPrimary).LinkToPrevious = False

' If the document already contains an Identifier1 field then
delete that field:
For Each fld In Selection.HeaderFooter.Range.Fields
If InStr(1, fld.Code, strFieldCode, vbTextCompare) Then
fld.Delete
End If
Next

' Add the Identifier1 field:
ActiveWindow.View.ShowFieldCodes = True
With Selection
.HomeKey Unit:=wdStory
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:="IF ", PreserveFormatting:=True
.MoveRight Unit:=wdCharacter, Count:=5
.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
.TypeText Text:=" = "
.Fields.Add Range:=Selection.Range, Type:=wdFieldSectionPages
.TypeText Text:=" AND "
.Fields.Add Range:=Selection.Range, Type:=wdFieldSection
.TypeText Text:=" = "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _
"DOCPROPERTY ""SectionsCount"" ", PreserveFormatting:=True
.TypeText Text:=" "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _
"DOCPROPERTY ""Identifier1"" ", PreserveFormatting:=True
Selection.HeaderFooter.Range.Fields.Update
End With
End If

' Move to the next section of the document:
If intCounter <> intSectionCounter Then
ActiveDocument.ActiveWindow.View.NextHeaderFooter
End If
intCounter = intCounter + 1
Loop

' Turn field codes off:
ActiveWindow.View.ShowFieldCodes = False

' Close the footer and return to document:
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Exit Sub

Err_Handler:
Select Case Err.Number
Case 5
‘ Create the CustomDocumentProperty named SectionsCount if it
does not already exist:
ActiveDocument.CustomDocumentProperties.Add
Name:="SectionsCount", LinkToContent:=False, _
Type:=msoPropertyTypeString, Value:=""
Resume Next
Case Else
‘ Display the error message and exit the macro:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly +
vbCritical, "Error"
Exit Sub
End Select
End Sub

Thank you.
 
G

Greg Maxey

Michael,

What is the IdentifierReference property?

I have adapted a nice little bit of code that Dave Lett provided the other
day to display conditional text on the last section/last page only. Maybe
you can use it for your purposes:
Dim oMyRng As Range
Public Sub LastPageOnlyFieldTextInFooter()
With ActiveDocument.Sections.Last
Set oMyRng = .Footers(wdHeaderFooterPrimary).Range
fInsertText oMyRng
Set oMyRng = .Footers(wdHeaderFooterEvenPages).Range
fInsertText oMyRng
Set oMyRng = .Footers(wdHeaderFooterFirstPage).Range
fInsertText oMyRng
End With
End Sub
Public Sub fInsertText(oFooterRng As Range)
Dim oFooterRng1 As Range
Dim oFooterRng2 As Range
Dim oFooterRng3 As Range
With oFooterRng
.Collapse Direction:=wdCollapseEnd
.Text = "IF PAGE = NUMPAGES ""Text here"""
Set oFooterRng1 = .Duplicate
Set oFooterRng2 = .Duplicate
Set oFooterRng3 = .Duplicate
End With
fInsertFields oFooterRng1, "PAGE"
fInsertFields oFooterRng2, "NUMPAGES"
fInsertFields oFooterRng3
End Sub
Public Sub fInsertFields(oMyRng As Range, Optional sText As String)
With oMyRng
With .Find
.Text = sText
.MatchCase = True
.Execute
End With
With .Fields
.Add Range:=oMyRng, Type:=wdFieldEmpty, _
PreserveFormatting:=False
.Update
End With
End With
End Sub
 
M

Michael Stauffer

The IdentifierReference is a field choice that is avaiable in the Header and
Footer toolbar under the Insert Auto Text tool. It supplies that document
name to the field.

I was able to solve my problem using nested If expressions within the field
code. Here is an example of the code used to create the field code:

With Selection
' First IF expression:
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="IF ",
PreserveFormatting:=False
.MoveRight Unit:=wdCharacter, Count:=5
.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
.TypeText Text:=" = "
.Fields.Add Range:=Selection.Range, Type:=wdFieldSectionPages
.TypeText Text:=" "
' Second (NESTED) IF expression:
.TypeText Text:=Chr(34)
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="IF ",
PreserveFormatting:=False
.MoveRight Unit:=wdCharacter, Count:=5
.Fields.Add Range:=Selection.Range, Type:=wdFieldSection
.TypeText Text:=" = "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:="DOCPROPERTY ""SectionsCount"" "
.TypeText Text:=" "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:="DOCPROPERTY ""Identifier1"" "
.TypeText Text:=" " & Chr(34) & Chr(34)
.MoveRight Unit:=wdCharacter, Count:=2
.TypeText Text:=Chr(34)
End With

Thanks for your help.
 

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