PDA

View Full Version : Solved: Update fields in last section only on save event



bstephens
11-29-2009, 02:56 AM
I'm trying to write a macro that will update the fields in the last SECTION of documents based on a template on a user save event.

I've been parsing through code trying to figure this out and I got as far as the following code which is supposed to update the FOOTER on a save event.

Private Sub oApp_DocumentBeforeSave(ByVal Doc As Document, _
SaveAsUI As Boolean, Cancel As Boolean)
Sub UpdateFooter()

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.WholeStory
Selection.Fields.Update
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

End Sub

This code doesn't seem to work.

Does anyone have an idea how to write a macro where word updates the fields in the last SECTION of the document only before a save event?

I am in Word 2007.

Thanks for any ideas.

Brian

macropod
11-29-2009, 03:18 PM
Hi Brian,

Dim rngHF As Range
With ActiveDocument.Sections.Last
For Each rngHF In .Footers
rngHF.Fields.Update
Next
For Each rngHF In .Headers
rngHF.Fields.Update
Next
.Range.Fields.Update
End With

fumei
11-30-2009, 11:55 AM
I take it you only want to update the fields in the headers and footer of the last Section...but not any fields in the document part of the last Section?

macropod
11-30-2009, 01:42 PM
Hi fumei,

If your supposition is correct, Brian could delete '.Range.Fields.Update' from my code.

fumei
11-30-2009, 01:53 PM
I do not actually have a supposition. Just a little bit curious.

The OP seems to imply all fields (document, and headers/footers) in the last Section:

"update the fields in the last SECTION of documents"

in which case your code applies...

Yet, the code posted only applies to the headers/footers (using View).

Just curious, is all.

bstephens
11-30-2009, 02:29 PM
Thanks for the input guys, this answered my question.