Working with File Save

D

David Bassett

My users want the name of the document file to appear in
the header whenever they save the document. The problem
I have is whenever they re-save the document. The code
would add a second instance of the filename date field.
The code I have now completely fails to put the date
field in at all. I need for the code to not add but
update or delete and reinsert the file name whenever the
users save.
Sub FileSave()

ActiveDocument.Save

Dim myrange As Range
Dim aField As Field
Set myrange = ActiveDocument.Sections(1).Footers _
(wdHeaderFooterPrimary).Range
For Each aField In myrange.Fields
aField.Delete
Next aField

Dim myrange1 As Range
Set myrange1 = ActiveDocument.Bookmarks
("FooterFileName").Range
With myrange1
.Fields.Add Range:=myrange, Type:=wdFieldFileName
End With

End Sub

TIA!
david
 
A

Anne Pontillo

You don't need VBA code to do this. In the document (or the template that
the documents are based on), insert the AutoText entry to do this. Most
people would put this in the header or footer area. To do so, choose View,
Header and Footer. On the Header/Footer toolbar, click the AutoText button
and choose either the "Filename" field which only inserts the filename, or
the "Filename and Path" field which will insert the complete path and
filename. This inserts a field codee that will automatically update
whenever the document name is changed (if you use the second option it will
also change if the doc is saved to a different directory with the same name.
Close the Header/Footer area. If the document has not been saved yet, this
field code will display as "Document1". Once you save the doc it will
display the name you have chosen.

Anne P.
 
D

David Bassett

I'm sorry that I did not emphasize this but my users
don't ever want to see "Document1" in the footer. They
want the file name to appear only after the document has
been saved. So I do need to do this in VBA code. What
is the problem with my code?
 
D

Doug Robbins - Word MVP

See response to your later post above.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

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