Automatically update

A

Anni

Hi
I have a field for "last save by" in document. When I save, I would this
field should automatic update.Please help!
 
G

Graham Mayor

Most fields do not 'automatically' update. You have to force an update. You
could intercept the FileSave and FileSaveAs routines in the document
template to update the fields on save eg

Sub FileSave()
Dim oStory As Range
ActiveDocument.Save
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
End Sub

and

Sub FileSaveAs()
Dim oStory As Range
Dialogs(wdDialogFileSaveAs).Show
ActiveWindow.Caption = ActiveDocument.FullName
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
End Sub

should work for most circumstances.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Shane Day

I am trying to trap all save as events so that I can load a form and input some data when a file is being saved with a new name.

The FileSaveAs routine can be used for this and works well in Word 2007. However, if the SaveAs - Word Document menu tab is clicked on instead of the SaveAs menu item the event is not trapped. Does anyone know how to trap this event as well.
 
D

Doug Robbins - Word MVP

If the default save format is Word 97-2003, you will need to name the macro
FileSaveWord11()

If the default is Docx then you will need to use FileSaveWordDocx()

Actually, you will probably need to have 3 macros - FileSaveAs(),
FileSaveWord11() and FileSaveWordDocx() (plus possibly FileSaveWordDotx()
and maybe even more as in Word 2007, there are now 10 different save
commands for various formats, etc).
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

Still_Learning

Thank you this helped me out.

Graham Mayor said:
Most fields do not 'automatically' update. You have to force an update. You
could intercept the FileSave and FileSaveAs routines in the document
template to update the fields on save eg

Sub FileSave()
Dim oStory As Range
ActiveDocument.Save
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
End Sub

and

Sub FileSaveAs()
Dim oStory As Range
Dialogs(wdDialogFileSaveAs).Show
ActiveWindow.Caption = ActiveDocument.FullName
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
End Sub

should work for most circumstances.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

pdguest

Thank you this helped me out.








- Show quoted text -

I have a text box in the header with fields in it that I'd like to
update automatically. This code doesn't seem to touch fields in any
text boxes. Is there a way to scan for all text boxes in a document
and automatically update the fields located in them as well?

Thanks!
-- Paul
pdguest who is at gmail.com
 
G

Graham Mayor

Some fields can be difficult to update. However the following code will
update REF fields in the header and in text boxes.

Dim sView As String
Dim sUpdate As String
sView = ActiveDocument.ActiveWindow.View.Type
sUpdate = Options.UpdateFieldsAtPrint
Options.UpdateFieldsAtPrint = True
Application.ScreenUpdating = False
PrintPreview = True
PrintPreview = False
ActiveDocument.ActiveWindow.View.Type = sView
Options.UpdateFieldsAtPrint = sUpdate
Application.ScreenUpdating = True

You can use that in place of the update code in the previous macros.
 

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