Mourning the loss of our friend, WhitPhil.
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Business Applications
Tag Cloud
access audio black screen blue screen boot bsod connection crash dell desktop driver drivers dvd email error excel firefox hard drive hardware hijackthis internet keyboard laptop malware monitor motherboard network networking outlook problem processor recovery registry cleaner router safe mode slow sound spyware tdlwsp.dll trojan vba video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Business Applications >
Solved: MS Word Macro - Before Save

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
DrewMcK's Avatar
Junior Member with 18 posts.
 
Join Date: Jan 2007
19-Jan-2007, 10:41 AM #1
Solved: MS Word Macro - Before Save
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's Avatar
OBP OBP is online now
Computer Specs
Distinguished Member with 9,341 posts.
 
Join Date: Mar 2005
Location: UK
Experience: An old Basic Programmer
19-Jan-2007, 02:51 PM #2
Before every save or "On Close" with automatic Save?
DrewMcK's Avatar
Junior Member with 18 posts.
 
Join Date: Jan 2007
19-Jan-2007, 03:08 PM #3
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's Avatar
OBP OBP is online now
Computer Specs
Distinguished Member with 9,341 posts.
 
Join Date: Mar 2005
Location: UK
Experience: An old Basic Programmer
19-Jan-2007, 03:13 PM #4
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.
__________________
OBP
I do not give up easily

Last edited by OBP : 19-Jan-2007 03:19 PM.
DrewMcK's Avatar
Junior Member with 18 posts.
 
Join Date: Jan 2007
19-Jan-2007, 04:02 PM #5
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's Avatar
Distinguished Member with 3,730 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Brilliant When Sober
20-Jan-2007, 01:01 PM #6
Replace the "CallUpdate" command with a messagebox to see if the before save event is even firing.

Regards,
Rollin
DrewMcK's Avatar
Junior Member with 18 posts.
 
Join Date: Jan 2007
20-Jan-2007, 04:46 PM #7
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's Avatar
Distinguished Member with 3,730 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Brilliant When Sober
20-Jan-2007, 05:58 PM #8
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

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

Regards,
Rollin

Last edited by Rollin_Again : 20-Jan-2007 06:13 PM.
DrewMcK's Avatar
Junior Member with 18 posts.
 
Join Date: Jan 2007
22-Jan-2007, 10:49 AM #9
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's Avatar
Computer Specs
Junior Member with 4 posts.
 
Join Date: Jul 2007
Experience: Beginner
24-Jul-2007, 01:07 AM #10
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's Avatar
Distinguished Member with 3,730 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Brilliant When Sober
24-Jul-2007, 10:11 AM #11
Quote:
Originally Posted by Rottnest
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
Rottnest's Avatar
Computer Specs
Junior Member with 4 posts.
 
Join Date: Jul 2007
Experience: Beginner
24-Jul-2007, 08:49 PM #12
Here it is (I think)
Attached Files
File Type: zip A - Bankruptcy Notice (User Form).zip (33.9 KB, 55 views)
Rollin_Again's Avatar
Distinguished Member with 3,730 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Brilliant When Sober
25-Jul-2007, 10:32 AM #13
What info are you trying to update the footers with?

Regards,
Rollin
Rottnest's Avatar
Computer Specs
Junior Member with 4 posts.
 
Join Date: Jul 2007
Experience: Beginner
25-Jul-2007, 08:46 PM #14
The reference number. I have a bookmark for it in the document and a field for it in the footer.
Rollin_Again's Avatar
Distinguished Member with 3,730 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Brilliant When Sober
26-Jul-2007, 09:54 AM #15
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.

Code:
Call UpdateFooter
Regards,
Rollin
Attached Files
File Type: zip A - Bankruptcy Notice (User Form).zip (20.8 KB, 92 views)
Closed Thread Bookmark and Share

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 12:33 PM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.