Inserting filename field in document

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Inserting filename field in document

Post by dasadler »

Word 2007 - I know how to insert the filename field but is there ny way to do it such that the extension (.docx or .doc) is not shown?
Don

User avatar
HansV
Administrator
Posts: 78241
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Inserting filename field in document

Post by HansV »

Word does not have built-in support for that, but Macropod posted some code you can use in the Windows Secrets Lounge: How to display filename without extension.
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Inserting filename field in document

Post by StuartR »

HansV wrote:Word does not have built-in support for that, but Macropod posted some code you can use in the Windows Secrets Lounge: How to display filename without extension.
That macro looks like it will only work if the filename has exactly one dot
If you have a filename like
StuartR edited this file.yesterday.sothere.doc
then it will just give you the text
StuartR edited this file
StuartR


User avatar
HansV
Administrator
Posts: 78241
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Inserting filename field in document

Post by HansV »

Here is a modified version of Macropod's code (it should go into the ThisDocument module, and you need to create a bookmark named Filename manually where you want the filename to be displayed):

Code: Select all

Private Sub Document_Open()
  Dim strBookmark As String
  Dim strFilename As String
  Dim rng As Range
  Dim intPos As Integer
  strBookmark = "FileName"
  strFilename = ActiveDocument.Name
  If ActiveDocument.Bookmarks.Exists(strBookmark) Then
    intPos = InStrRev(strFilename, ".")
    If intPos > 0 Then
      strFilename = Left(strFilename, intPos - 1)
    End If
    Set rng = ActiveDocument.Bookmarks(strBookmark).Range
    If Not rng.Text = strFilename Then
      rng.Text = strFilename
      ActiveDocument.Bookmarks.Add strBookmark, rng
    End If
  Else
    MsgBox "Bookmark: " & strBookmark & " not found.", vbExclamation
  End If
  Set rng = Nothing
End Sub
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Inserting filename field in document

Post by dasadler »

Thanks Hans. That does work just fine. One question, though, when I insert a bookmark, it does not give any indication of a bookmark having been inserted until I reopen the document (in this case). In general, however, is there any way to display bookmarks (a bookmark symbol, for example)? so I would know if I have already inserted the bookmark?
Don

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Inserting filename field in document

Post by StuartR »

You can display bookmarks from

Word Options > Advanced, look in the "Show document content" section for "Show bookmarks"

Each bookmark will appear as a pair of [ ] around the bookmarked range.
StuartR


dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Inserting filename field in document

Post by dasadler »

Thanks Stuart. In looking at that code, I am curious as to why it is designed to present the message box if the bookmark does not exist. Seems like it would be more efficient to simply create the bookmark if it did not already exist then it would be available to the user when needed.
Don

User avatar
HansV
Administrator
Posts: 78241
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Inserting filename field in document

Post by HansV »

The code runs automatically when the document is opened. It doesn't "know" where you would want the bookmark if it doesn't exist.
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Inserting filename field in document

Post by dasadler »

Through experimentation, it seems I can only use that bookmark once. In other words, if I use it as part of a footer, then place the bookmark in the body of the document, it longer longer appears in the footer.

Maybe I am using the wrong words here but I thought that the bookmark can be defined as the filename then used at will... sort of like a user defined function. Does this mean a bookmark refers to a specific place int he document?
Don

User avatar
HansV
Administrator
Posts: 78241
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Inserting filename field in document

Post by HansV »

You can insert the bookmark in one place only, but you can refer to it elsewhere in the document by using a REF field:
- Click where you want the filename sans extension to appear too.
- Press Ctrl+F9 to insert field braces { } (do *not* type them yourself).
- Between the braces, enter the following text (literally):

REF Filename

- Press F9 to hide the field codes and update the field.
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Inserting filename field in document

Post by dasadler »

Got it. Thanks.
Don