1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: MS Word Macro - Before Save

Discussion in 'Business Applications' started by DrewMcK, Jan 19, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. DrewMcK

    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.
     
  2. OBP

    OBP

    Joined:
    Mar 8, 2005
    Messages:
    19,895
    Before every save or "On Close" with automatic Save?
     
  3. DrewMcK

    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
     
  4. OBP

    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.
     
  5. DrewMcK

    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?
     
  6. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    Replace the "CallUpdate" command with a messagebox to see if the before save event is even firing.

    Regards,
    Rollin
     
  7. DrewMcK

    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?
     
  8. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    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
     
  9. DrewMcK

    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.
     
  10. Rottnest

    Rottnest

    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.
     
  11. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    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
     
  12. Rottnest

    Rottnest

    Joined:
    Jul 24, 2007
    Messages:
    4
    Here it is (I think)
     

    Attached Files:

  13. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    What info are you trying to update the footers with?

    Regards,
    Rollin
     
  14. Rottnest

    Rottnest

    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.
     
  15. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    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:

  16. Sponsor

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 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/536573

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice