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

by Allen Wyatt
(last updated November 22, 2014)

6

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 in 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 2010 and 2013.

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

Inserting Summary Information

Want to insert into your document those snippets of information that you know Word maintains about your document? It's ...

Discover More

Determining the Current Directory

When creating macros, it is often necessary to know which directory is the default. Here's how you can find out by using ...

Discover More

Deriving High and Low Non-Zero Values

When analyzing your numeric data, you may need to figure out the largest and smallest numbers in a set of values. If you ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Floating Footer

Need some specific text to appear just below the end of the text on the last page of your document? You can accomplish ...

Discover More

Changing Headers and Footers

Editing what is in your page header or footer is fairly easy, and you can use the same editing techniques you already ...

Discover More

Deleting a Header or Footer

When working with existing documents, you may need to delete a header or footer previously created. Here's how you can do ...

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}] 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 0 + 2?

2018-01-05 07:41:25

Shane H

Great tip, Allen. Thanks.

Is there a way to do the above but for every footer, rather than the current footer? I'm looking to put the part of the file name on every footer in quite a large document.

Thanks.


2015-01-14 19:51:19

Linda M

Diane Palmersheim
Not sure if I added this to my AutoText, but all I have to do is go to the footer and type "Filename and path" (without the quotes) and press F3. This puts in the whole path. If you just want the Filename, type "Filename" then press F3. I think these are defaults for MS Word but not sure.

Following are a couple macros to add to the Normal.dotx then add the macro your Quick Access toolbar. The macro will move from the body of your document and add the field into the footer of your document. This works for MS Word 2010.

Sub Insert_Filename_Footer()
'
' Insert_Filename_Footer Macro
'
'
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 = wdSeekCurrentPageFooter
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

Sub Insert_FilenamePath_Footer()
'
' Insert_FilenamePath_Footer Macro
'
'
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 = wdSeekCurrentPageFooter
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME p ", PreserveFormatting:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

--Linda


2014-11-28 15:37:24

Diane Palmersheim

Is there a macro that can be assigned to a button to automatically put the document path in the footer?


2014-11-24 10:38:32

Anne

Hi Robert, I think fastest way to leave entire path is to go
1) to footer, click on Quick Parts in the Insert portion of the Header&Footer Tools ribbon,
2) click on field,
3) Under Categories, select Document Information
4) under Field Names select FileName
5) Field Properties - format - whatever you prefer
6) Field Options, check box for Add Path to Filename


2014-11-22 13:09:32

awyatt

Robert: See this tip:

http://wordribbon.tips.net/T007552

-Allen


2014-11-22 12:53:24

Robert Quinn-O\'Connor

Good tip. Is there a way to leave the whole document name, including the path, in the footer?


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.

Newest Tips
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.