Displaying a document's file name only on the last page

With a little field magic, you can create an expression that displays a document's path and field name only on the document's last page.

Some organizations require that you display a document's file name in a header or footer. I've seen this request complicated though by special requests. For instance, do you know how to display a document's name only on the last page? Fortunately, this task is easily accomplished using Word fields.

The fields you need are {PAGE} and {NUMPAGES}—you're probably already familiar with them. But instead of dropping them into your document in the usual Page x of y format, you need an expression that compares the result of both fields to determine if the current page is also the last page. To insert this expression in a header of footer, do the following:

  1. Click the Insert tab and then click Header or Footer in the Header & Footer group. In Word 2003, choose Header and Footer from the View menu.
  2. Type IF followed by a space character.
  3. Click the Insert tab and click Quick Parts in the Text group and choose Field. In Word 2003, choose Field from the Insert menu. (Or, click Insert Page Number on the Header and Footer toolbar. If you do so, skip the next step, but only in Word 2003.)
  4. From the Field Names list, choose Page and click OK. Word will display either the literal page number or the {PAGE} field.
  5. After the page field, enter a space character, an equal sign, and another space character.
  6. Repeat steps 3 and 4, but insert the {NUMPAGES} field. Don't worry if your expression doesn't look exactly like mine. Word will display the literal page numbers or the field codes.
  7. Enter a space character after the number of pages field.
  8. Repeat steps 3 and 4, selecting the {FILENAME} field from the Field Names list.
  9. In the Field options section of the Field dialog box, check the Add Path to Filename option (or not, depending on your needs), and click OK.
  10. Select the entire expression and press [Ctrl]+[F9] to create a custom field.
  11. Press [F9] to calculate the custom field.

If you're on the last page, the field will display the document's path and filename. If you aren't on the current page, the expression will display nothing.

Inserting fields is easy enough, but turning a combination of fields into a custom field, as I've shown you how to do, can be a little awkward the first time or two you do it. It's easy to insert an extra field and not realize it's there. If you see a spare set of brackets, you know that's what you've done. Display the fields (select the custom field and press [F9]), select the extra field, and delete it.