Consulting

Results 1 to 7 of 7

Thread: word 2007 - automatically update fields in footer after save event

  1. #1

    word 2007 - automatically update fields in footer after save event

    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

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  3. #3
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    991
    Location
    [VBA]Private Sub FileSave()
    Dim rngStory As Range
    For Each rngStory In ActiveDocument.StoryRanges
    With rngStory
    .Fields.Update
    End With
    Next rngStory
    End Sub
    [/VBA]

    David


  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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?
    [vba]
    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
    [/vba]

  5. #5
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    991
    Location
    Quote Originally Posted by fumei
    Tinbendr, you forgot to do the file saving instruction!
    DOH!

    David


  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,672
    Location
    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 ...
    Cheers
    Paul Edstein
    [MS MVP - Word]

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •