WD2000: How to Print a Field Result on the Last Page of a DocumentID: q209220
|
This article describes how to automatically place the file name of the current document (or any other field result) on the last page of every document based on the Normal template or, using a Visual Basic for Applications macro, just the current document.
if { page }={ numpages } "{ filename \p }"so that the field looks like this:
{ if { page } = { numpages } "{ filename \p }" }NOTE: The \p switch causes the entire path to the file name to be included. The field brackets are actual fields, and the above code cannot be recreated with copy and paste. They should be entered as separate fields. In other words, each set of brackets corresponds to a field created with CTRL+F9.
http://www.microsoft.com/mcsp/For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/refguide/
Sub PutFileNameOnLastPage()
' Are any documents open?
If Documents.Count = 0 Then Exit Sub
' Go to footer of document in the last section
' of the document.
Selection.EndKey Unit:=wdStory
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
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
' If the document already contains a filename, exit this routine.
For Each ao In Selection.HeaderFooter.Range.Fields
If ao.Type = wdFieldFileName Then
MsgBox "This document already contains a filename."
Exit Sub
End If
Next
' Add the Filename field.
ActiveWindow.View.ShowFieldCodes = True
With Selection
.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:=wdFieldNumPages
.TypeText Text:=" "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME \p "
Selection.HeaderFooter.Range.Fields.Update
End With
' Turn field codes off.
ActiveWindow.View.ShowFieldCodes = False
' Close the footer and return to document.
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
Q212536 OFF2000: How to Run Sample Code from Knowledge Base Articles
For more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:
Q226118 OFF2000: Programming Resources for Visual Basic for Applications
Additional query words: vb vba vbe
Keywords : kbcode kbprg kbdta kbdtacode kbmacroexample wd2000
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbinfo
Last Reviewed: May 26, 1999