# Solved: MS Word Macro - Before Save



## DrewMcK (Jan 12, 2007)

This is sort of relates to my other thread, but is for word this time so I figured it deserves its own thread.

I need a macro in MS Word, that will, before saves, update footer fields. This is mainly to automatically update the timestamp without having to go into print preview.

the update all keyboard command does not apply to headers / footers it seems.
I found a macro that will update all including headers and footers.

Sub UpdateAll()
Dim oStory As Range
Dim oField As Field
On Error Resume Next
For Each oStory In ActiveDocument.StoryRanges
For Each oField In oStory.Fields
oField.Update
Next oField
Next oStory
On Error GoTo 0
End Sub



I just need to a macro to launch the above macro before every save.

Thanks.


----------



## OBP (Mar 8, 2005)

Before every save or "On Close" with automatic Save?


----------



## DrewMcK (Jan 12, 2007)

Before every save. 

With excel the first line of code in the workbook code for the before save function i have is:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

But it appears to be different in Word...

If I could get the code so that on every save it launches another macro that would be perfect. And then from there I can change the macro that is to be run on every save if need be, or add others to it.

Im new to this, and was starting to get a feel for Excel, but im just not sure the different commands in word as opposed to excel.

Thanks


----------



## OBP (Mar 8, 2005)

I am not that familiar with word but here it is.
Private Sub appWord_DocumentBeforeSave _
(ByVal Doc As Document, _
ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
Here is a big tip for you, just in case you don't know already (and a lot of people don't know this).
The VB Editor has a completely different "Help" from each of the Programs that it runs in.
So Access, Excel and Word all have their own "Helps" but they all also have their own Unique VBA Editor Help and that is where you can find these commands.

Personally I prefer to use the "On Close" as I explained in Widgeboy's Excel post, as it only runs the once when you have finished making all the changes.


----------



## DrewMcK (Jan 12, 2007)

So, here is the code I have...

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



Call UpdateAll


End Sub

Sub UpdateAll()
Dim oStory As Range
Dim oField As Field
On Error Resume Next
For Each oStory In ActiveDocument.StoryRanges
For Each oField In oStory.Fields
oField.Update
Next oField
Next oStory
On Error GoTo 0
End Sub


When I save it, it doesn't update, but when i go into the VBA editor and run, it does update, so it seems to have a hard time calling the updateall command. 

Any thoughts?


----------



## Rollin_Again (Sep 4, 2003)

Replace the "CallUpdate" command with a messagebox to see if the before save event is even firing.

Regards,
Rollin


----------



## DrewMcK (Jan 12, 2007)

Its doesn't seem to be bringing up the msg box when I put it in the before save procedure, therefore not firing..... any other thoughts on the beforesave command for word?


----------



## Rollin_Again (Sep 4, 2003)

Follow these steps:

Open the VB editor. In the project explorer Window right click "ThisDocument" and select "View code" and copy and paste the code below into the code window.


```
Private Sub Document_Open()

Call Register_Event_Handler

End Sub
```
Next, in the VB editor insert a new blank module (INSERT >> MODULE) and copy and paste the code below into the new blank module .


```
Dim X As New Class1
Public Sub Register_Event_Handler()
    Set X.App = Word.Application
End Sub
```
Next, in the VB editor insert a Class Module (INSERT >> CLASS MODULE) and copy and paste the code below into the blank class module. Make sure when the class module is inserted it has the name Class1.

```
Public WithEvents App As Word.Application

Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)

MsgBox ("BEFORE SAVE TEST")

End Sub
```
Now save the document and close it. You will need to re-open the document to register the class and application objects before you can click the SAVE button to test. Just replace the messagebox in the class module with your code :up:

Hope this helps! If this solves your problem feel free to mark as solved using the thread tools above.

Regards,
Rollin


----------



## DrewMcK (Jan 12, 2007)

Rollin, you are the man.
I never wouldve been able to come up with the module and class module stuff myself. After using your code, of course the message box came up everytime, but for some reason I would have to save twice in order for the fields to update. I threw the save command before my code to update, and Success works like a charm.


----------



## Rottnest (Jul 24, 2007)

Dear Rollin_Again

I have just joined after seeing this post. I have a similar problem to Drew.

In my case I have a userform in Word but I wish to use that same userform to update my footers. Unfortunately, I can't make the updatefooter macro fire when I press the OK button on my userform.

can you tell me how this should be done. At present I have my macro in a separate module.

Any help will be appreciated.

Greg.


----------



## Rollin_Again (Sep 4, 2003)

Rottnest said:


> Dear Rollin_Again
> 
> I have just joined after seeing this post. I have a similar problem to Drew.
> 
> ...


Is there any reason why your macro is stored in the module? Your code to update the footers would normally be stored under the button click event. I'll need to take a look at your code before I can provide a suggestion. Can you post your document? If you are unable to post it please email to rollin_again at hotmail dot com

Regards,
Rollin


----------



## Rottnest (Jul 24, 2007)

Here it is (I think)


----------



## Rollin_Again (Sep 4, 2003)

What info are you trying to update the footers with?

Regards,
Rollin


----------



## Rottnest (Jul 24, 2007)

The reference number. I have a bookmark for it in the document and a field for it in the footer.


----------



## Rollin_Again (Sep 4, 2003)

You simply need to add the following line to the end of your button click event. I've added the line for you and re-saved and attached the doc.


```
Call UpdateFooter
```
Regards,
Rollin


----------

