Solved: MS Word Macro - Before Save

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

DrewMcK

Thread Starter
Joined
Jan 12, 2007
Messages
18
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.
 

DrewMcK

Thread Starter
Joined
Jan 12, 2007
Messages
18
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

Joined
Mar 8, 2005
Messages
19,895
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

Thread Starter
Joined
Jan 12, 2007
Messages
18
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?
 
Joined
Sep 4, 2003
Messages
4,916
Replace the "CallUpdate" command with a messagebox to see if the before save event is even firing.

Regards,
Rollin
 

DrewMcK

Thread Starter
Joined
Jan 12, 2007
Messages
18
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?
 
Joined
Sep 4, 2003
Messages
4,916
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.

Code:
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 .

Code:
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.
Code:
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 (y)

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

Regards,
Rollin
 

DrewMcK

Thread Starter
Joined
Jan 12, 2007
Messages
18
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.
 
Joined
Jul 24, 2007
Messages
4
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.
 
Joined
Sep 4, 2003
Messages
4,916
Rottnest said:
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.
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
 
Joined
Jul 24, 2007
Messages
4
The reference number. I have a bookmark for it in the document and a field for it in the footer.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Top