Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Limiting Directories in the FILENAME Field.
Written by Allen Wyatt (last updated January 22, 2022)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021
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.docx
You might want to limit the directory levels displayed, as shown in these two examples:
\Level2\Level3\Level4\Doc1.docx \Level1\Level2\Level3\Level4\Doc1.docx
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 insert, 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.docx
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 or just format it as hidden text.
Note:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13292) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Word here: Limiting Directories in the FILENAME Field.
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!
Need to step through the fields in a document? It's easy using the shortcuts detailed in this tip.
Discover MoreWorking with today's date in Word is easy. Trying to manipulate dates to come up with a future one can be an entirely ...
Discover MoreThe TC field is normally used in constructing manual Tables of Contents. The way the field works, however, makes it a ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments