save event

C

Craig

Hello,

Can someone tell me if I can hook into the save event. For some reason
fields located in my header and footer don't update when I do: CTRL+A;
F9. So on a save event, I want to add the code that will find all
fields and update them.

Thanks,
Craig H.
 
T

Tony Jollans

Yes you can if you want - just create a macro called FileSave in a standard
module and it will replace the built-in Save action.

To update fields in Headers and Footers you can just switch to Normal View
and (back) to Print Layout - and you can set Word to automatically update
fields on printing so the latest values are always printed. Also, page
number fields in headers and footers update in real time without any action
from you.
 
G

Greg

Craig,

I use this code and I have it assigned to run on pressing F9. I don't
bother with the CTRL+a step.

Sub myUpdateFields()
Dim pRange As Word.Range
For Each pRange In ActiveDocument.StoryRanges
Do
pRange.Fields.Update
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next
End Sub
 
J

joelfinkle

One recommendation for an "update all fields" function:
I hate seeing the "Page Numbers or Whole Thing" message when TOCs update, so
I've added

For Each toc In ActiveDocument.TablesOfContents
toc.Update
Next toc
For Each toa In ActiveDocument.TablesOfAuthorities
toa.Update
Next toa
For Each tof In ActiveDocument.TablesOfFigures
tof.Update
Next tof

This prevents those awkward little dialog boxes.

Another hint: If you have deleted or added tables and figures, you may find
your TOC doesn't match the table and figure numbers, even after running
either Word's "Update Fields" or the code below (with or without my trick
above). This is because things happen top-to-bottom, so the TOC's get
updated, then the field codes for the captions. So use my code above after
the fields are updated, and it will complete the updates.

Joel Finkle, PMP
Director Product Strategy, Image Solutions, Inc.
 
C

Craig

Hello,

Can someone tell me if I can hook into the save event. For some reason
fields located in my header and footer don't update when I do: CTRL+A;
F9. So on a save event, I want to add the code that will find all
fields and update them.

Thanks,
Craig H.

Hi,

Tony, Greg: thanks for your answers. I've taken advice from both and
here's what I came up with.

The only issue remaining is if I update a field then use "Save As...",
the newly saved doc doesn't display the most recent changes in the
field. I could solve my problem by knowing how to kick off the "Save
As..." with vba, after calling my UpdateFields_ method.

Any help is appreciated,
Craig.


Sub AutoNew()
ApplyTemplateSettings_
End Sub

Sub AutoOpen()
ActiveWindow.Caption = ActiveDocument.FullName 'Display filename
and path in window title bar
UpdateFields_
ApplyTemplateSettings_
End Sub

Sub FileSave()
UpdateFields_
ActiveDocument.Save
End Sub

Sub UpdateFields_()
Dim pRange As Word.Range
For Each pRange In ActiveDocument.StoryRanges
Do
pRange.Fields.Update
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next
ActiveDocument.AttachedTemplate.Saved = True
End Sub

Sub ApplyTemplateSettings_()
ActiveWindow.ActivePane.DisplayRulers = True 'display the rulers
ActiveWindow.ActivePane.View.ShowAll = False 'turn off the
formatting command view
With ActiveWindow.View
.Type = wdPrintView 'select page/print layout view
'.Type = wdNormalView 'Alternative to the above line if normal
view is preferred
.Zoom.Percentage = 100 'set the display zoom to 100%
.FieldShading = wdFieldShadingWhenSelected 'set the field
shading preference
.ShowFieldCodes = False 'turn off field code display
.DisplayPageBoundaries = True 'Turn on white space between pages
End With
CommandBars("Reviewing").Visible = False 'Turn off the reviewing
toolbar
Options.UpdateFieldsAtPrint = True 'Update fields before printing
Options.UpdateLinksAtPrint = True 'Update links before printing

'CustomizationContext = NormalTemplate
CustomizationContext = ActiveDocument.AttachedTemplate
Set kbTemp = KeyBindings.Key(BuildKeyCode(wdKeyF9))
If (kbTemp Is Nothing) Then
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyF9), _
KeyCategory:=wdKeyCategoryMacro, Command:="UpdateFields_"
Else
FindKey(BuildKeyCode(wdKeyF9)).Disable
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyF9), _
KeyCategory:=wdKeyCategoryMacro, Command:="UpdateFields_"
End If
ActiveDocument.AttachedTemplate.Saved = True
End Sub
 
D

DSUK

Hi you could also try

ActiveDocument.Fields.Update
This will update all fields in the document

alternatively there is a setting in Tools/Options/General and Update link on
open, this will update your fields when the document is opened.

If you really need to hook into the save event then you are looking at
something a little more complicated and will need a startup template that has
a class module and hooks into Words events, try looking in the help files in
word VBA and type in "BeforeDocumentSave Event", this will give you a
starting point
 

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