Add a filename field without .doc extension in document header
Working in Office 2007, I'd like to add a filename field in my document header. The documen开发者_如何转开发t 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.
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.
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...):
- insert filename without extension .doc using fields?
- Inserting a File Name without an Extension
add [Title] Instead of filename and maybe add some of the Other Document Properties Listed
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 Field like:
Done.
Indeed there is no "basename only" option for the FileName field.
Long term: Feature request
For the long term I've 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 up-vote that suggestion.
Short term workarounds
Macro (VBA code)
Writing a macro, as @DennisG has suggested, is probably the most convenient work around.
Custom property
But you may want to avoid associating any macro with your word document to avoid security issues (e.g. if you distribute your document). So another workaround is to create custom property FileBaseName
with a hard coded value "MyDocumentBaseName":
Create the FileBaseName custom property and assign it your value:
- Open a blank document in MS Word.
- Save it as "MyTempReport.docx".
- In MS Word Menu with MyTempReport.docx open > File [tab] > Info > Properties: Click > Advanced Properties ... > Custom [tab] ...
- Type in "Name": "FileBaseName"; "Value": "MyTempReport".
Insert the custom document property, FileBaseName
, in your document:
- MS Word menu > Insert [tab] > Text [group] > Quick Parts > Field ...
- From "Field Names" choose "DocProperty".
- From "Field Properties" > "Property": Choose "FileBaseName" > OK.
You should now have "MyTempReport" inserted as a field in your document.
Limitations:
- If you rename your file this won't be reflected in your
FileBaseName
field. Instead you'll have to manually change theFileBaseName
property from Advanced Properties ... (as above). - You'll have to create this custom property in every particular document you want it in.
If, and only if, the file name will not change in length, you may change the font of the extension to be "Hidden":
- to insert the field go to Insert » Quick Parts (Text) » Field…, then select "FileName" from the list (in category "Document Information") and ensure the check mark at Preserve formatting during updates is ticked (this appends the option
\* MERGEFORMAT
to the field codes; when this is missing, the following Hidden font effect will disappear every time the field value becomes updated); - select the shown file name extension (including the
.
); - manoeuvre to Home » Font (arrow at lower left corner), then tick the check box Hidden;
I am using this method for files names like the following, where only the revision code may change:
ID_REV_some_descriptive_name.doc?
, whereREV
is just a single letter likeA
,B
,C
, etc.;
Insert the file name field as shown in the diagram above and save as filename. View in print preview mode and return to edit mode: filename should now show instead of Documentn
Once you have the file name you can format different parts with different colors: so change the color of the .extension to the background color of the document (white probably).
I did this in a footer so that the pdf version didn't show '.docx'.
Just hide the file name extensions in Windows Explorer. After that reopen the document. Then export it. The name will not have the extension. :)
In order to have the document name automatically update in the fields, you need to add a little more code to your macro. This macro will check what the filename is every time you open the file and will update all the fields in which you want the filename to be seen.
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 you create this macro, simply go back to the document:
select 'Quick Parts' in the 'Insert' tab, and click on 'Field.'
In the Field options, select DocVariable, and type in fname into Field Properties and then click OK.
It will then insert your file name, sans extension, into your doc, updating every time you open the file, and display the new filename.
精彩评论