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: Inserting a File Name without an Extension.

Inserting a File Name without an Extension

Written by Allen Wyatt (last updated April 15, 2020)
This tip applies to Word 97, 2000, 2002, and 2003


4

You can use the FILENAME field to insert the current document's file name in your document. The exact way you use this field is described in other issues of WordTips. You can use field switches to specify whether you want the field results to include the full path for the file, or not. One thing you cannot specify, however, is whether you want the results to exclude the file extension.

One way you can control this is by configuring Windows to either display or hide filename extensions. You do that within Windows itself by displaying a folder on your hard drive and then choosing Tools | Folder Options and displaying the View tab.

Scroll through the list of viewing settings, and make sure that the Hide Extensions for Known File Types is set the way you want it. If the check box is clear, then Windows (and Word) always displays the file extension. If it is checked, then the file extension is not displayed.

This approach works great as long as the documents are being viewed on your system. If the document is opened on someone else's system, then the FILENAME field will display the file extension according to the configuration of Windows that they have set up; this may be a drawback.

An easy way to insert the file name without the extension is to use a different field. For instance, you could use File | Properties to set the Title field to the document name (type it in manually) without the extension. You could then use the DOCPROPERTY field to recall that title and insert it in your document.

If you need to insert the document name quite often, the best way to do it is with a macro. Consider the following single-line macro:

Sub InsertFileName()
    Selection.InsertBefore Text:=Left(ActiveDocument.Name, _
      Len(ActiveDocument.Name) - 4)
End Sub

Run this macro, and the name of your document (without the file extension) is inserted before whatever is selected in your document. The macro examines the document name, and then strips the last four characters (the period and file extension) from the name. This is what is inserted.

Using this approach is very easy, but it isn't dynamic. This means that if the document name is changed, then the text in the document still reflects the old document name, not the new one. The way around that is to simply run the macro again to insert the new document name at whatever point you want it to appear.

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 (292) 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: Inserting a File Name without an Extension.

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

Spell Checking Your Worksheet

One of the indicators of a well-done worksheet is if there are any spelling errors within it. Excel allows you to easily ...

Discover More

Changing the Size of a Drawing Object

Documents are often made up of more than just text. If you have drawing objects in your document, you will doubtless need ...

Discover More

Deleting Everything Up to a Character Sequence

Sometimes you have too much information in a cell and you need to "pare down" what is there to get to the info you really ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (menu)

Use Filenames That Sort Properly

If you take some care when you name your document files, you'll find it much easier to manage those files at a later ...

Discover More

Changing Information in Multiple Documents

If you need to change text in many documents at the same time, Word isn't the best tool to use. Here's some ideas on ways ...

Discover More

Can't Open a Word Document in Windows

Double-click on a Word document while using Windows Explorer, and the Word program should start with the document ...

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 8 - 5?

2017-05-22 05:10:09

Alan Elston

EDIT:
Actually I forgot the important extra
VBA.
bit on my Len ....

This would be my unpolished version
Selection.InsertBefore Text:=VBA.Left$(ActiveDocument.Name, VBA.Len(ActiveDocument.Name) - 4)

:)


2017-05-22 05:05:49

Alan Elston

I would tend to prefer the “unpolished” in a final code. I would tend to prefer to keep it simpler to follow later as well as possibly being a bit quicker.
I might then in the ‘Comments hidden to the right include a fuller version and fuller explanation

The following is how I would do it. The uncommented part is almost the same as Allen’s ( If you copy it all to the code window it does not look as bad as the ‘Commented parts going off to the right in a long single row, and so you only see them for later reference if you scroll to the right ). So my opinion best of both worlds.

Sub InsertFileNameEP()
Selection.InsertBefore Text:=VBA.Left$(ActiveDocument.Name, Len(ActiveDocument.Name) - 4) ' Text:=VBA.Strings.Left$(ActiveDocument.Name, (VBA.Strings.InStrRev(ActiveDocument.Name, ".", -1, vbBinaryCompare) - 1)) ' The $ sometimes thought to a bit quicker as it expects a string as argument, it may then error - for the case of Null as argument. I get the length of up to the extension point by InStrRev looking from right but counting position from the left ( in the string , looking from the right for a point character "." , -1 means start looking from the last character , vbBinaryCompare is case sensitive and possibly a bit quicker ) then take away 1 to get the length to just before the point VBA. Will ensure we go to the correct Library, as sometimes (in XL mostly between XL2007 and XL2010 I have seen codes have errored for string functions as they go to the wrong default library )
End Sub


Allen’s version is fine in my opinion to demo the basic idea. My long ‘comments are just a personal preference

Alan
:-)


2017-05-21 05:13:19

Petyka

Wow. Removing the extension by subtracting 4 from the length. How unpolished...


2016-06-12 13:17:02

Vik

How do I append the value (in a bookmark), to the file name?

Say, file name is 123.doc.

bookmark value is Richard

I need result , filename to be changed to 123Richard.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.