Hi Karen
Steve's advice is good. You also need to consider that Word has three
different footers for each section. So you really need to add the fields to
all 3 footers. And, you might want to append the fields to existing text in
the footers. I recently had to do something similar for someone.
Here's some code adjusted for your circumstances.
If you're not sure what to do with it, see
http://www.gmayor.com/installing_macro.htm
You can use Tools > Customize to create a button on a toolbar to invoke the
ShowFileNameOnLastPage macro.
Sub ShowFileNameOnLastPage()
'Shauna Kelly, 18 December 2004
'This version for microsoft.public.word.vba.general
'Insert fields { if { page ) = {numpages} {filename}}
'in the footers for the last section of the active document.
Dim nSectionCount As Long
Dim bFooterHasPageNumber As Boolean
Dim bAppendFooter As Boolean
Dim bOldShowFieldCodes As Boolean
With ActiveDocument
With .Windows(1).View
'Store the user's choice about displaying field codes
bOldShowFieldCodes = .ShowFieldCodes
'Display field codes, or none of this will work
.ShowFieldCodes = True
End With
'Find out which is the last section in the document
nSectionCount = .Sections.Count
'Find out whether the footer already has a page
'number field
bFooterHasPageNumber = _
FooterHasPageNumber(nSection:=nSectionCount, _
eFooter:=wdHeaderFooterEvenPages) _
Or FooterHasPageNumber(nSectionCount, _
eFooter:=wdHeaderFooterFirstPage) _
Or FooterHasPageNumber(nSectionCount, _
eFooter:=wdHeaderFooterPrimary)
'Work out whether to replace the existing footer, or append
'the page number to the existing footer
bAppendFooter = True
If bFooterHasPageNumber Then
If MsgBox("The last section of this document" _
& " already has page numbers." _
& vbCrLf & vbCrLf _
& "Replace existing footers in this section?", _
vbQuestion + vbYesNo) = vbYes Then
bAppendFooter = False
End If
End If
'Insert the file name fields into all 3 footers
'in the last section of the document
InsertFileNameFields _
nSection:=nSectionCount, _
eFooter:=wdHeaderFooterEvenPages, _
bAppendFooter:=bAppendFooter
InsertFileNameFields _
nSection:=nSectionCount, _
eFooter:=wdHeaderFooterFirstPage, _
bAppendFooter:=bAppendFooter
InsertFileNameFields _
nSection:=nSectionCount, _
eFooter:=wdHeaderFooterPrimary, _
bAppendFooter:=bAppendFooter
'Return the display of field codes to the user's preference
.Windows(1).View.ShowFieldCodes = bOldShowFieldCodes
End With
End Sub
Function FooterHasPageNumber(nSection As Long, _
eFooter As WdHeaderFooterIndex) As Boolean
'Return True if this footer contains a page number field
Dim oField As Field
FooterHasPageNumber = False
With ActiveDocument
For Each oField In .Sections(nSection).Footers(eFooter).Range.Fields
If oField.Type = wdFieldPage Then
FooterHasPageNumber = True
End If
Next oField
End With
End Function
Function InsertFileNameFields(nSection As Long, _
eFooter As WdHeaderFooterIndex, bAppendFooter As Boolean) As Boolean
'Inserts fields for { if { page ) = {numpages} {filename}} into the eFooter
'of section nSection.
'If bAppendFooter is true, then append to the existing footer.
'Otherwise, replace the existing footer.
'There is no return value.
Dim oFooterRange As Word.Range
Set oFooterRange = ActiveDocument. _
Sections(nSection).Footers(eFooter).Range
If bAppendFooter Then
If Len(oFooterRange.Text) > 1 Then
'Inset a carriage return to put the page number on a new line
oFooterRange.InsertAfter vbCrLf
End If
Else
'Empty the existing footer
With oFooterRange
.Text = ""
.ParagraphFormat.Reset
End With
End If
'Insert the field codes
With oFooterRange
.Collapse wdCollapseEnd
.Fields.Add Range:=oFooterRange, _
Type:=wdFieldIf, PreserveFormatting:=False
.Move Unit:=wdWord, Count:=2
.Fields.Add Range:=oFooterRange, _
Type:=wdFieldPage, PreserveFormatting:=False
.Move Unit:=wdWord, Count:=3
.Move Unit:=wdCharacter, Count:=-1
.InsertAfter " = "
.Collapse wdCollapseEnd
.Fields.Add Range:=oFooterRange, _
Type:=wdFieldNumPages, PreserveFormatting:=False
.Move Unit:=wdWord, Count:=3
.Move Unit:=wdCharacter, Count:=-1
.Fields.Add Range:=oFooterRange, _
Type:=wdFieldFileName, PreserveFormatting:=False
End With
End Function
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
> G'day "Karen Clark" <anonymous@discussions.microsoft.com>,
>
[quoted text clipped - 84 lines]
>>Still WAY too new to know what I'm doing...
>>Karen