Insert the filename field in the footer

H

hb

I'd like to insert the filename field in the footer after a string ("File
Name:-").

thanks in advance.
 
J

Jezebel

Assuming you've already read the Help page on Filename fields, what problem
have you run into?
 
H

hb

I do a mailmerge in Word 2003 which removes all the really useful stuff like
bookmarks and fields by default. So, I need a way of inserting the filename
field in the footer in a specific place so that when the user does a 'save
as' they get the file name. I thought I'd use the 'File Name:-' string as a
pseudo-bookmark and place the filename field after it.
 
H

hb

Ultimately, I want {FileName \p} to appear after the 'File Name:-' string
like so

File Name:-c:\test.doc

So, I want to modify this code, which just inserts the file name field in
the start of the footer, to find the 'File Name:-' string and insert the
field after it.

Sub PutFileNameOnLastPage()
' Are any documents open?
If Documents.Count = 0 Then Exit Sub
' Go to footer of document in the last section
' of the document.
Selection.EndKey Unit:=wdStory
If ActiveWindow.ActivePane.View.Type = wdNormalView Or _
ActiveWindow.ActivePane.View.Type = wdOutlineView Or _
ActiveWindow.ActivePane.View.Type = wdMasterView Then
ActiveWindow.ActivePane.View.Type = wdPageView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
' If the document already contains a filename, exit this routine.
For Each ao In Selection.HeaderFooter.Range.Fields
If ao.Type = wdFieldFileName Then
' Just exit
Exit Sub
End If
Next
' Add the Filename field.
ActiveWindow.View.ShowFieldCodes = True
With Selection
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME \p "
Selection.HeaderFooter.Range.Fields.Update
End With

Regards

HB
 
J

Jezebel

Dim pRange As Word.Range

Set pRange = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
pRange = "Filename: "
pRange.Collapse Direction:=wdCollapseEnd
pRange.Fields.Add Range:=pRange, Type:=wdFieldFileName, Text:="\p"
 
H

hb

Thanks for that but there is one slight problem. There is a table with
various information (ie. VAT no. etc) in the footer of the document and this
appears to remove it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top