PDA

View Full Version : Solved: Update FILENAME and SAVEDATE fields automatically



clhare
08-04-2009, 08:06 AM
I have a document that uses the SAVEDATE field (both in the text and the footers) and the FILENAME field (used only in the footers). I have set the FILENAME fields to include the file path.

I am trying to get all these fields to update automatically when the document is opened. I have put the following code in ThisDocument:

Private Sub Document_Open()
Dim aStory As Range
Dim aField As Field

' Automatically update all fields in document when it is opened
For Each aStory In ActiveDocument.StoryRanges
For Each aField In aStory.Fields
aField.Update
Next aField
Next aStory
End Sub


When I test the document by changing the date in the date fields and then saving/closing then moving it to another folder, I find that the dates are updating correctly but the folder is not. How do I get the FILENAME field to update in all the footers?

lucas
08-04-2009, 08:28 AM
Maybe:

Private Sub Document_Open()
With ActiveDocument
.Unprotect
.Fields.Update
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End With
End Sub

clhare
08-04-2009, 09:03 AM
Nope, it's still not updating the FILENAME field.

lucas
08-04-2009, 09:23 AM
This should help Cheryl:

http://www.vbaexpress.com/kb/getarticle.php?kb_id=459

clhare
08-04-2009, 09:35 AM
That works! Thank you!

macropod
08-04-2009, 06:16 PM
Hi Cheryl,
Here's a far simpler macro for you to use:
Sub UpdateFields()
Application.ScreenUpdating = False
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
Application.ScreenUpdating = True
End Sub

clhare
08-10-2009, 05:08 AM
Great! Thanks so much!