Illustration: Lisa Hornung, Getty Images/iStockPhoto

If someone asked you to add a conditional header or footer to a document in Microsoft Word, would you panic? Some users might because the interface offers no such option. You can use the Different Odd & Even Pages option, but that’s not the same thing.

A conditional header or footer will display, or not, depending on whether an expression returns TRUE or FALSE. In this article, I’ll show you three ways to use fields to generate a conditional header or footer, although the examples will all be in the footer.

SEE: 83 Excel tips every user should master (TechRepublic)

I’m using Microsoft 365 on a Windows 10 64-bit system, but you can use earlier versions. Word fields have been around for a long time. This article assumes you have basic Word skills, but even a beginner should be able to follow the instructions to success. You can download the demonstration .docx and .doc files, but I encourage you to practice entering the fields yourself. Word Online doesn’t display headers and footers; nor can you manipulate them. However, they are still there, and they will print.

What’s a field in Word?

If you’re familiar with Word fields, you’re a step ahead. If not, they’re not difficult to use, but the process might feel a bit odd at first. Fields are little programs that complete a specific task, similar to a macro, but you don’t need to write any code. Everything the field needs to do its job is already there, except for the information you supply.

You use fields almost every time you use Word, but you just don’t realize it. For instance, when you use the interface to display a page number in the header or footer, you’re using a field! If you’d like to see that at work, open a new document and enter a page number in the header. Then, select the page number and press Alt + F9. Word converts the page number to its underlying { PAGE } field. In this mode, you can modify the field. Press Alt + F9, which acts as a toggle, again to see the page number.

The expression to display on last page of the Word doc

It’s common to display a different header or footer on the first or last page of a document. You can use the built-in Different First Page option to display a different header on the first page. There’s no such option for the last page; determining the last page requires a simple field expression in the form

{ IF {PAGE} = { NUMPAGES }, “true text” “false text”}

It’s easy to decipher: If the current page number, { PAGE } equals the total number of pages, { NUMPAGES } print “true text”. If the two page numbers don’t match, the expression prints “false text”.

Let’s look at a quick example using a three-page document:

  1. Open the header or footer by double-clicking in the top or bottom margin.
  2. Word will position the cursor at the left margin, which is fine.
  3. In this next part, we’ll be entering the fields manually, which I think is the easiest way to enter nested fields. To insert a blank field, press Ctrl + F9 and type IF. (Case doesn’t really matter.)
  4. While still inside the If field, press Ctrl + F9 a second time to enter a blank field and enter PAGE.
  5. Move the cursor to the right of the closing } character in the { PAGE } field and enter =.
  6. Press Ctrl + F9 a third time and enter NUMPAGES.
  7. Move the cursor to the right of the closing } character in the NUMPAGES field and enter “Last page” “Not the last page”.

Figure A shows the nested fields. Do not enter the {} characters from the keypad; it won’t work. You must enter the blank fields using Ctrl + F9 and enter the content inside those brackets. Now, select the entire nested field expression and press Alt + F9 to evaluate the results (Figure A). As you can see, the last page is the current page.

Figure A

I’ve shown you only one way to enter fields, but there are other methods. Before we continue, I want to share another way to enter nested fields that you might find easier. You can type the field name, such as IF or NAME, select that name and press Ctrl + F9. Doing so will convert IF or NAME to a field and insert the brackets.

This simple expression works fine until you have unnumbered pages in your document. For instance, the document might have a table of contents or a title page that you don’t want in the page count. When this happens, you must find a way to accommodate those pages.

SEE: Windows 10: Lists of vocal commands for speech recognition and dictation (free PDF) (TechRepublic)

If you’re excluding only the first page, use the Different First Page option. Doing so will inhibit a page number, and Word won’t include that page in the final page count. Excluding other pages might require a section break or an expression that evaluates those pages. The following expression excludes a two-page section at the beginning of the document that’s not included in the total page count:

{ IF {PAGE} = { NUMPAGES – 2 } , “Last page” “Not the last page”}

The expression is almost the same as the first, but the -2 component subtracts 2, the total number of pages you want to exclude from the page count.

If you want the expression to return nothing, use the empty string character, “”. For example, the following nested field expression will return a string on the last page and return nothing on all other pages:

{ IF {PAGE} = { NUMPAGES } , “” “Not the last page” }

Now let’s look at a condition that seems a bit more complex.

How to specify page numbers in a footer in Word

What happens when you have a footer that you want to print only on specific pages? You use the same fields to express the specific page numbers and use the appropriate true and false text. Let’s suppose that you want to print a page number only on pages 5 and 6?

The problem only seems more complex, but in truth, it’s simpler. Instead of a nested expression, use two expressions:

{ If {Page } = “5” “Page 5” “” }

{ If {Page } = “6” “Page 6” “” }

Figure B shows the page number on page 6.

Figure B

Both conditions are simple to satisfy using two different expressions instead of a nested expression. Now, let’s look at something you might think impossible using fields.

How to display an image in a header or footer in Word

If asked if you can display a conditional image in the header or footer, you might reply, “No.” That’s not correct. You treat an image the same as any other text or value. First, you need to insert the image file into the document and save it as AutoText. Then, you insert the AutoText into the footer or header.

First, let’s add TechRepublic’s logo to the document as an AutoText element:

  1. Insert the picture into the body of the document (not a header or footer) as you would any other image file.
  2. Select the image and click the Explore Quick Parts dropdown.
  3. Choose Save Selection to Quick Parts Gallery.
  4. In the resulting dialog, give the image a name, such as TRLogo (see Figure C).
  5. Click OK.

Figure C

Now that you have the image as an AutoText item in the document, you can insert it into the header as follows:

  1. Position the cursor in the header or footer where you want it to appear.
  2. Using Figure D as a guide, enter the { IF } field up to the first ” character.
  3. Click the Insert tab.
  4. From the Explore Quick Parts dropdown (Text group), choose AutoText.
  5. Select the image (Figure E) and click OK.

Figure F shows the logo in the footer, conditionally; it appears only on the last page thanks to the conditional field expression shown in Figure D.

Figure D

Figure E

Figure F

This introduction to displaying conditional content, including images, in a document’s header or footer is just the beginning. You can use the following comparison operators: =, <>, <=, and >=. You’ve learned how to nest fields, which is extremely useful. However, even when you could use a nested expression, you don’t always have to, as shown in the second example. Remember to always use F9 to insert and modify fields.

Stay tuned

I mentioned earlier that there are more ways to enter fields than the one I worked you through in the first example. A future article will include all of the ways to enter Word fields.

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays