Add a file name field without the .doc extension in the document header

When working in Office 2007, I would like to add a file name field to my document title. In the future, the document will be a PDF, so I do not want the extension.

I played with Insert → QuickParts → Field, but to no avail. I have a feeling that it requires a formula ...

Thanks in advance if you can help.

+7
source share
7 answers

Your feeling is absolutely correct.

Insert > QuickParts > Field > FileName is the way to go, but as you can see in the screenshot below, you have no way to enable or disable the file extension. alt text To show or not to show (Shakespearean style) the extension simply depends on the settings of Windows Explorer to show or hide known file extensions. Thus, either you change this parameter, or you need some kind of code.

A very simple macro will be as follows:

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

What he does is simply cut the last 4 characters of the string "Filename string", for example. ".doc" - if you save ".docx", "." will be saved. Also, this macro will run once, and you will need to run it again when the file name changes.

Perhaps you could explain something else that you want to achieve by having the file name in the title of the document? Are you trying to use the file name in the document header to set some PDF property during conversion? Why not use the title of the document? You need the original PDF file name later - why?

Two more pages to help you solve your problem (both rely on macros ...):

+9
source

add [Title] Instead of a file name and maybe add some other Document Properties List

+6
source

Create a macro something like:

 Dim f As String f = Dir(ActiveDocument.FullName) intPos = InStr(1, f, ".") If intPos > 0 Then f = Left(f, intPos - 1) End If doc.Variables("BaseFileName").Value = f 

Insert a type field:

Insert field DocVariable

Done.

+3
source

Indeed, there is no “base name only” option for the FileName field.

Long Term: Feature Request

In the long run, I made a feature request with Microsoft at: https://office365.uservoice.com/forums/264636-general/suggestions/13860672-for-word-create-a-basename-only-field-option-for

I invite you and others visiting this post to vote for this proposal.

Short-term workarounds

Macro (VBA code)

Writing a macro, as @DennisG suggested, is probably the most convenient job.

Custom property

But you can not associate the macro with your text document in order to avoid security problems (for example, if you distribute your document). So, another workaround is to create a custom FileBaseName property with the hard-coded value "MyDocumentBaseName":

Create a custom FileBaseName property and set it to your value:

  • Open a blank document in MS Word.
  • Save it as "MyTempReport.docx".
  • In the MS Word menu with MyTempReport.docx, open> File [tab]> Information> Properties: click> Advanced properties ...> Custom [tab] ...
  • Enter "Name": "FileBaseName"; "Value": "MyTempReport".

Insert your own document property FileBaseName into the document:

  • MS Word menu> Insert [tab]> Text [group]> Quick parts> Field ...
  • In the Field Names box, select DocProperty.
  • In Field Properties> Property: select FileName> OK.

You should now add “MyTempReport” as a field in the document.

Limitations:

  • If you rename your file, this will not be reflected in your FileBaseName field. Instead, you will have to manually change the FileBaseName property in Advanced Properties ... (as described above).
  • You need to create this custom property in each specific document in which you want.
+1
source

Insert the file name field as shown in the diagram above and save as the file name. Preview in preview mode and return to edit mode: the file name should now be displayed instead of Documentn

Once you have the file name, you can format the different parts with different colors: so change the .extension color to the background color of the document (possibly white).

I did this in the footer so that ".docx" is not displayed in the pdf version.

0
source

Just hide the file name extensions in Windows Explorer. After that, open the document again. Then export it. The name will not have an extension. :)

0
source

In order for the document name to be automatically updated in the fields, you need to add a little more code to your macro. This macro will check the file name every time you open the file, and updates all the fields in which you want the file name to be displayed.

 Sub AutoOpen() ' AutoUpdate Macro which creates filename without extension and updates fields Dim aStory As Range Dim aField As Field Dim fname As String fname = ActiveDocument.Name fname = Left(fname, Len(fname) - 5) 'removes the last five characters from the filename (.docm) With ActiveDocument .Variables("fname").Value = fname End With For Each aStory In ActiveDocument.StoryRanges 'updates all fields in the document when you open the file For Each aField In aStory.Fields aField.Update Next aField Next aStory End Sub 

After creating this macro, just go back to the document:

  • select Quick Details on the Insert tab and click Field. select 'Quick Parts' in the 'Insert' tab, and click on 'Field.'

  • In the field options, select DocVariable, enter fname in the "Field Properties" field and click "OK." In the Field options, select DocVariable, and type in fname into Field Properties and then click OK.

Then it will insert your file name without extension into your document, updating every time you open the file, and display a new file name.

0
source

All Articles