PDA

View Full Version : word 2007 - automatically update fields in footer after save event



bstephens
01-14-2010, 01:42 AM
Does anybody know the best practice to automatically update the fields (specifically filename and path field) in a footer after the user clicks save. I thought I had this figured out awhile ago, but apparently I do not.

I have really struggled trying to find something that works and I'm not sure why I can't come up with a solution to what seems like a pretty basic problem.

Thanks in advance for any ideas.

Brian

fumei
01-14-2010, 11:07 AM
You could add update coding to the Save event itself. This means writing a FileSave event that includes both the field updating and the saving itself.

Tinbendr
01-14-2010, 11:13 AM
Private Sub FileSave()
Dim rngStory As Range
For Each rngStory In ActiveDocument.StoryRanges
With rngStory
.Fields.Update
End With
Next rngStory
End Sub

fumei
01-14-2010, 11:55 AM
Tinbendr, you forgot to do the file saving instruction!

That would do the update...but NOT do the actual saving of the file. The file would NOT be saved.

I am not a fan of going through all the StoryRanges, unless actually needed. Going through all the StoryRanges means looping through (assuming there are no Comments, or Endnotes):

MainTextStory
EvenPagesHeaderStory
PrimaryHeaderStory
EvenPagesFooterStory
PrimaryFooterStory
FirstPageheaderStory
FirstPageFooterStory

in that order. Since (again assuming you do NOT want to check through the Comments story, the EndNotes Story, the FootNotesStory or the TextFrameStory....then why not just action the stories you weant to explicitly?

Sub FileSave()
Dim oHF As HeaderFooter
Dim oSection As Section
Dim rngStory As Range

For Each oSection In ActiveDocument.Sections
For Each oHF In oSection.Headers
oHF.Range.Fields.Update
Next
For Each oHF In oSection.Footers
oHF.Range.Fields.Update
Next
Next

With ActiveDocument.Range
.Fields.Update
End With
ActiveDocument.Save
End Sub

Tinbendr
01-14-2010, 12:38 PM
Tinbendr, you forgot to do the file saving instruction!DOH! :doh:

macropod
01-14-2010, 04:43 PM
Of course, if you update the fields after saving, the state reverts to unsaved. And if one of those fields is a SAVEDATE field, you'll be chasing your tail forever ...

fumei
01-15-2010, 11:31 AM
That macropod, always finding the best part.

Although, that is why I did the update BEFORE the save. Because, then who cares? Except....for that darn SAVEDATE.

Good one.