Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Limiting Directories in the FILENAME Field.

Limiting Directories in the FILENAME Field

Written by Allen Wyatt (last updated June 4, 2021)
This tip applies to Word 97, 2000, 2002, and 2003


1

The FILENAME field allows you to insert the name of the document file into the document itself. If you use the /p switch with the field, you get not only the file name, but also the full path for the file:

{ FILENAME /p }

As you can imagine, the path name can get rather long, depending on how your hard drive is organized and where you stored the document. For this reason, you may want to selectively choose which levels of the path are included in what FILENAME returns. For instance, the following may be the full path name for the document:

C:\My Documents and Settings\Level1\Level2\Level3\Level4\Doc1.doc

You might want to limit the directory levels displayed, as shown in these two examples:

\Level2\Level3\Level4\Doc1.doc
\Level1\Level2\Level3\Level4\Doc1.doc

Unfortunately, there is no way to do this with the FILENAME field itself; it just doesn't include that capability. The only solution is to create a macro that determines the path name and inserts the desired levels into the document. For instance, the following macro will inert, at the insertion point, the desired number of directory levels for the current file:

Sub SelectPaths()
    Dim sPath As String
    Dim sName As String
    Dim sFull As String
    Dim sPart As String
    Dim sMsg As String
    Dim sTemp As String
    Dim iLevels As Integer
    Dim J As Integer

    sPath = ActiveDocument.Path
    If sPath = "" Then
        MsgBox "Need to save before running this macro.", _
          vbOKOnly, "This Document Not Saved"
    Else
        sPath = sPath & Application.PathSeparator
        sName = ActiveDocument.Name
        sFull = sPath & sName

        sMsg = "This is the full path:" & vbCrLf
        sMsg = sMsg & sFull & vbCrLf & vbCrLf
        sMsg = sMsg & "How many levels do you want, counting "
        sMsg = sMsg & "from right to left?"

        sTemp = InputBox(sMsg)
        iLevels = Val(sTemp)

        sPart = ""
        If iLevels > 0 Then
            For J = Len(sFull) To 1 Step -1
                If Mid(sFull, J, 1) = Application.PathSeparator Then
                    iLevels = iLevels - 1
                    If iLevels = 0 Then
                        sPart = Mid(sFull, J, 255)
                        Exit For
                    End If
                End If
            Next J
        End If

        Selection.TypeText (sPart)
    End If
End Sub

If the document has not been saved, the macro won't run. It works by essentially counting the number of path separators (slashes), starting at the end of the path. It then inserts just the part of the path from that point forward.

The drawback to a macro like this, of course, is that it is not dynamic, as fields are. It simply inserts text. If you later change the location of the document, or if you change the document name, then you need to rerun the macro to insert the new path text.

If your reasoning behind inserting only a portion of the path is that the path is too long when included in its entirety, there is another approach that you might take. Why not simply reduce the point size of the portion of the path that is not important. For instance, let's say that you use the FILENAME field to insert the path, and it appears like this:

C:\My Documents and Settings\Level1\Level2\Level3\Level4\Doc1.doc

If you want to hide the part to the left of "Level2," just select that text in the field results, and format it as a very small point size. If you make the point size something like 6 or 7 points, the de-emphasized portion is still legible, but the full path doesn't take up as much linear space in your document. If you want the de-emphasized portion to essentially disappear, you can set the point size to 1 point.

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 (6399) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Limiting Directories in the FILENAME Field.

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

Dynamic Hyperlinks in Excel

Hyperlinks to many types of Web sites rely on passing parameters in the URL. Knowing this, you can construct a dynamic ...

Discover More

Filling A Drawing Object

Creating a drawing object can be just the first step in putting together your masterpiece drawing. This tip explains how ...

Discover More

Converting to ASCII Text

When you work with imported or pasted data in an Excel worksheet, you may see some strange looking characters at times. ...

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 (menu)

Inserting the Total Number of Pages in Your Document

Word keeps track of many statistics for each of your documents. One statistic is the total number of pages in the printed ...

Discover More

Setting Up an Array with Fields

One of the more esoteric ways to display data is with an "array," which is like a miniature inline table. This tip ...

Discover More

Inserting the Document Creation Date

One of the pieces of information tracked by Word is when a document was first created. Here's how you can access that ...

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 two more than 7?

2017-04-24 22:49:18

GALENO VIEIRA ROCHA

Is it possible to insert just the folder of the file? No the filename and not the full path?

Like: Myfolder
instead of: C/Myfolder/file.doc


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon 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.