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

by Allen Wyatt
(last updated November 22, 2014)

5

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.

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

Getting Rid of Choppiness in Justified Text

Justified text doesn't always produce the best-looking results. Here's how to avoid some of the choppiness that can occur.

Discover More

Embedding TrueType Fonts in E-mails Composed in Word

When you use Word as your e-mail editor, it allows you to format the text of your e-mail messages using tools you are ...

Discover More

Adding Parentheses

Need to add parentheses around some word or phrase? Here's a quick macro that makes this simple edit in one step.

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (ribbon)

Including Headers and Footers when Selecting All

If you are creating a macro that, in the course of processing your document, needs to update all the fields in the document, ...

Discover More

Keeping Centered Headers and Footers Centered

Headers and footers provide a nice final touch for your printed documents. If you want to expertly align text in those ...

Discover More

Applying Standard Headers and Footers to a Document

When you attach a template to a document, it doesn't modify the documents headers or footers. This tip provides three ...

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 four less than 6?

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.