Using a Portion of a Document's Filename in a Header

Written by Allen Wyatt (last updated January 8, 2022)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


Vladimir knows that he can put a document's filename into the header of a page. What he would like, though, is to only have the last five characters of the filename (exclusive of the filename extension) appear in the header. He is casting about for the best way to accomplish the task.

The only way to do this is by using a macro; there is no built-in functionality in Word to accomplish the task. The macro needs to determine the filename, grab the characters desired, and then stuff then into the header. There are potential complications with something that might seem so simple, though. For instance, what if the document hasn't been saved and therefore has no filename yet? What if the document has been saved, but there are fewer than five characters in the filename? What should the macro do if there is already something in the header? What should it do if the user is looking at the document in a view that doesn't display headers?

To deal with such questions, the macro needs to make some assumptions. For this example, we'll assume that the macro should simply replace whatever existing header there is with the desired portion of the filename. Further, the macro can switch the viewing mode to Print Layout view so the header is easy to work with. Here's the result:

Sub PartFilenameInHeader()
    Dim sName As String
    Dim J As Long

    sName = ActiveDocument.Name
    J = InStrRev(sName, ".")
    If J > 0 Then
        sName = Left(sName, J - 1)
        If Len(sName) > 5 Then
            sName = Right(sName, 5)
        End If

        If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            ActiveWindow.Panes(2).Close
        End If
        If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
          ActivePane.View.Type = wdOutlineView Then
            ActiveWindow.ActivePane.View.Type = wdPrintView
        End If
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        Selection.WholeStory
        Selection.Delete
        Selection.TypeText Text:=sName
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    Else
        MsgBox "Document has no filename extension."
    End If
End Sub

The macro first checks to ensure that the document has a real filename (from the .Name property of the ActiveDocument object). If so, then it pulls five (or fewer) characters from the filename. It checks to make sure there are not multiple panes open and that the document is in Print Layout view. It then selects whatever is currently in the header and replaces it with the desired characters from the filename.

Understand that the macro should be run once after saving the document and once again if you ever save the document using a different filename.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13316) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Turning Off Highlighter Display

You can use the highlighter tool to add all sorts of color to your document. If you want to turn off those colors so that ...

Discover More

Excluding Some Data from a Chart

Excel is a whiz at creating charts from your worksheet data. When the program tries to determine what should be included ...

Discover More

Creating Thin Spaces

Thin spaces are a typographic device that allows you add a bit of space between elements of a document. There are no thin ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Pulling Headers and Footers from Other Files

You may have some standard headers and footers you want to make available in your document templates. This tip describes ...

Discover More

Changing the Type of Page Numbers Used in Headers or Footers

Like to have your page numbers displayed using different types of numbers? Here's how you can choose from the several ...

Discover More

Protecting Headers and Footers

If you don't want the information in a header or footer to be changed by users of your document, there are a couple of ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 0?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.