Software

How to display the filename and path in a Word document or title bar

You can display the name of a Word document in the header or footer--or even in the title bar-- but the results are a little rough around the edges. See what works and what doesn't.

hero
Image: iStockphoto.com/Jacob Ammentorp Lund
When you open a Word document, Word displays its name in the title bar. If it's a new blank document, Word displays a generic name, documentx, until you name the file. That's adequate for most of us, but occasionally, you'll want the document's name to print with the content or you'll want the entire path in the title bar. Unfortunately, both requirements often leave users scratching their heads in frustration. That's because what seems simple has some unexpected kinks. In this article, I'll show you solutions to these requirements and warn you when they don't work as expected.

I'm using Word 2016 on a Windows 10 64-bit system, but you can apply these solutions all the way back to Word 2003. You can use any file you want or you can download the demonstration .doc, .docm, and .cls files. The solutions discussed in this article aren't compatible with 365.

Use {FILENAME} field

When you want to display the document's name and path in the file itself, you can type it, but if you change the filename, you must remember to update the name in your document. If you use the {FILENAME} field instead, it doesn't update automatically either. You must remember to update the field—how is that any better than typing it manually? One advantage is clear when you're working with multiple instances of a filename: Updating the field once updates it throughout the document.

Let's use this field to display the filename in a document's header and see what happens:

  1. Double-click the header area to open it in edit mode and position the cursor where you want to display the filename. In Word 2003, choose Header And Footer from the View menu.
  2. Click the Insert tab and choose Field from the Quick Parts dropdown in the Text group (Figure A). In Word 2003, you'll find this setting in the AutoText options.
  3. Select FileName from the Field Names list.
  4. If you want to display the full path, check the Add Path To FileName Option (Figure B).
  5. Click OK to return to the document.
  6. Double-click outside the header to close the header.

Figure A

Figure A

Field codes are part of the Quick Parts feature.

Figure B

Figure B

You can display the filename with or without the full path.

As you can see in Figure C, Word displays the name and path in the document's header, so it'll be visible on every page of the document. Later, I'll show you how to display it conditionally.

Figure C

Figure C

Word displays the filename and path in this document's header.

Unfortunately, if you save the file using a different name, you must update the field manually to display the new name. To do so, open the header, right-click the field, and choose Update Field (Figure D). If you're using our demo file, be sure to open the header and update the field to see the correct path for your system.

Figure D

Figure D

Update the field if you change the file's name.

If you're likely to forget to update the field and it's important that the file print with the right name, you can set a print option to update fields before printing, as follows:

  1. Click the File tab.
  2. Click Print in the left pane.
  3. Click Page Setup at the bottom.
  4. On the Paper tab, click Print Options.
  5. In the Printing Options section, check Update Fields Before Printing (Figure E).
  6. Click OK twice.

Figure E

Figure E

Set this option to update fields before printing the document.

Without a lot of effort, this is the best you can hope for. You might consider adding an ActiveDocument.Fields.Update event to the FileSaveAs event, but the timing's wrong and it won't work. VBA updates the fields before saving the new name. The file won't update the field the next time you open it either, so you might consider adding the Update event to an AutoOpen macro, so at the very least, the document displays the correct name when opened. However, doing so won't help if the field is in the header or footer. The update method updates fields only in the document body.

There's no silver bullet because each document will be unique regarding fields. Since our example field is in the header, we'll add a VBA procedure that updates fields in the header (or footer). Add the sub procedure in Listing A to the document's AutoOpen event in the ThisDocument module as follows:

  1. Open the Visual Basic Editor (VBE) by pressing [Alt]+[F11] or clicking Visual Basic in the Code group on the Developer tab.
  2. Click ThisDocument (if necessary) in the Explorer window.
  3. Enter the code in Listing A. To avoid errors in the VBE, don't cut and copy the code from this web page. Enter the code manually or download the demonstration document or class file.
  4. Return to the document and save it as a macro-enabled file. If you're using Word 2003, this step isn't necessary.

Listing A

Sub AutoOpen()

'Update field in header on first page.

ActiveDocument.Sections(ActiveDocument.Sections.Count). _

Headers(1).Range.Fields.Update

End Sub

Opening the document triggers the AutoOpen procedure and the Update method updates only the fields in the header section. The value 1 represents the page number. If the header isn't on page 1, update this value argument. If you're using the footer section, change Headers(x) to Footers(x). It's a can of worms you'd almost rather not open, but for simple documents, this solution works.

Conditional display

You might not want to display the filename on every page; perhaps you'd like to see it only on the last page, which seems reasonable. When this is the case, you can wrap the field in an {IF} expression. For instance, the following expression will display the filename only on the last page of the document:

{ IF { PAGE } = { NUMPAGES } { FILENAME \P \* MERGEFORMAT } }

This is just one of many possibilities. We're not going to cover an extensive list of expressions, but it's important to know that expressions give you more flexibility and control over where the document displays the field.

SEE: Microsoft Office 365: The smart person's guide

Use VBA for title bar display

Displaying the filename in the document will be adequate for most of us. If you need a bit more, consider using VBA to display the full path and filename in the title bar. Using the directions from above to open the VBE, add the code in Listing B and Listing C to a document when you want to display the full pathname in the title bar... except, the solution doesn't always work.

Listing B

Sub FileSaveAs()

'Display file name in title bar.

'Usurps save as command.

If Dialogs(wdDialogFileSaveAs).Show = 0 Then Exit Sub

System.Cursor = wdCursorNormal

ActiveWindow.Caption = ActiveDocument.FullName

End Sub

Listing C

Sub AutoOpen()

'Display filename in title bar.

ActiveWindow.Caption = ActiveDocument.FullName

End Sub

Saving the file triggers an internal event, FileSaveAs. The code is Listing B usurps this event and allows Word to display the document's FullName property in the title bar. For better or worse, this simple macro most likely will not display the full path; I have never seen it work consistently. Because Word will display the document's name automatically, there's little use for it. I've included it for the sake of discussion only—it's one of those kinks I mentioned. If you must have the pathname in the title bar, you can find an excellent solution by Graham Mayor, MVP in a discussion at the Microsoft Office Community. The good news is that the AutoOpen event in Listing C does display both the filename and path in the title bar, as you can see in Figure F, when you open the file. Unfortunately, you lose it as soon as you save the file.

Figure F

Figure F

The AutoOpen event displays the path; the FileSaveAs event usually doesn't.

No panacea

These solutions might satisfy your simplest requirements, but they have inherent problems that are difficult to work around. Next month, watch for a solution that displays the file's location on the Quick Access Toolbar. It's certainly easier, even though it works only in the most recent versions.

Send me your question about Office

I answer readers' questions when I can, but there's no guarantee. Don't send files unless requested; initial requests for help that arrive with attached files will be deleted unread. When contacting me, be as specific as possible. For example, "Please troubleshoot my workbook and fix what's wrong" probably won't get a response, but "Can you tell me why this formula isn't returning the expected results?" might. Please mention the app and version that you're using. I'm not reimbursed by TechRepublic for my time or expertise when helping readers, nor do I ask for a fee from readers I help. You can contact me at susansalesharkins@gmail.com.

Also read...

About Susan Harkins

Susan Sales Harkins is an IT consultant, specializing in desktop solutions. Previously, she was editor in chief for The Cobb Group, the world's largest publisher of technical journals.

Editor's Picks

Free Newsletters, In your Inbox