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.
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.