Add a filename field without .doc extension in document header

Ben picture Ben · Oct 13, 2010 · Viewed 38.9k times · Source

Working in Office 2007, I'd like to add a filename field in my document header. The document will later be a PDF, so I don't want the extension.

I've played with Insert -> QuickParts -> Field, to no avail. I've got a gut feeling that it requires a formula...

Thanks in advance if you can help.

Answer

Dennis G picture Dennis G · Oct 13, 2010

Your feeling is quite right.

Insert > QuickParts > Field > FileName is the way to go, but as you see from the screenshot below, you do not have the option to turn the file extension on or off. alt text To show or not to show (Shakespearean style) the extension is purely up to the Windows Explorer setting to show or hide known file extensions. So, either you change that setting or you need some code.

A very simple Macro would be the following:

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

What it does is simply strip the last 4 characters of the "Filename string" e.g. ".doc" - if you safe a ".docx" the "." would be preserved. Also this Macro would run once and you would need to run it again when the filename changes.

Maybe you could explain some more what you want to achieve with having the filename in the document header? Are you trying to use the filename in the document header to set some PDF property during conversion? Why not use the Document Title? Do you need the original filename in the PDF later - why?

Two more pages to help you with your problem (both relying on Macros...):