automatically update inserted file name

M

Milton Ross

The filename has been inserted into a document. I want the filename to update
automatically when saved as another name. Is this possible?
 
C

Charles Kenyon

Not without putting a macro into your file. If the field is in a
header/footer, though, it will be updated when you print or do a print
preview.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
C

Charles Kenyon

Greg, I made a couple of modifications to your macro because I sometimes use
fields like Fill-In fields that I do _not_ want to update. My revision is
below.

Option Explicit

Sub FileSaveAs()
' Based on macro shown in Greg Maxey's page at
' http://gregmaxey.mvps.org/File_Name_And_Path.htm
'
' Displays filename and path in document's title bar when saved using Save
As
' Also updates filename fields in document
'
Dialogs(wdDialogFileSaveAs).Show
System.Cursor = wdCursorNormal
ActiveWindow.Caption = ActiveDocument.FullName
Dim oStory As Range
Dim oField As Field
For Each oStory In ActiveDocument.StoryRanges
' oStory.Fields.Update ' replace this with the following For-Next
loop
For Each oField In oStory.Fields
If oField.Type = wdFieldFileName Then
oField.Update
End If
Next oField
If oStory.StoryType < wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oField = Nothing
Set oStory = Nothing
ActiveDocument.Save ' Saves document with updated field(s)
End Sub

In my limited testing, it seems to work. I also added the final save
command. Of course, in a document with lots of fields my changes are going
to make the macro take a more time, I expect.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
C

Charles Kenyon

Because I don't like to mess with normal.dot and couldn't figure out how to
do a global pseudo AutoOpen in a different global template, I added the
following intercept to my global. It gives the filename and path on a
document save.

Sub FileSave()
' Based on macro shown in Greg Maxey's page at
' http://gregmaxey.mvps.org/File_Name_And_Path.htm
'
' Displays filename and path in document's title bar when saved using Save
' Also updates filename fields in document
'
ActiveDocument.Save
System.Cursor = wdCursorNormal
ActiveWindow.Caption = ActiveDocument.FullName
Dim oStory As Range
Dim oField As Field
For Each oStory In ActiveDocument.StoryRanges
' oStory.Fields.Update ' replace this with the following For-Next
loop
For Each oField In oStory.Fields
If oField.Type = wdFieldFileName Then
oField.Update
End If
Next oField
If oStory.StoryType < wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oField = Nothing
Set oStory = Nothing
ActiveDocument.Save ' Saves document with updated field(s)
End Sub

--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
G

Greg

Charles,

Good points. Next time I get around to working on the web page I will
make these changes. Thanks.
 

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