Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 25

Thread: Solved: Running macro on word document close, stopping close

  1. #1
    VBAX Mentor OTWarrior's Avatar
    Joined
    Aug 2007
    Location
    England
    Posts
    389
    Location

    Solved: Running macro on word document close, stopping close

    I am trying to put some data checking into a word document to check for a formfield value, and if the formfield is empty, but only on document close

    with the warning I need it to prompt the user, and to not allow closure of the document until this field is filled in.

    I have the code to do the check, and to give the warning message, but don't know how to stop closing the document.

    Here's what I have:

    [vba]Private Sub Document_Close()

    With ActiveDocument
    If .FormFields("chkMale").CheckBox.Value = False And _
    .FormFields("chkFemale").CheckBox.Value = False Then
    MsgBox "Please say what gender this person is", vbInformation, "~Missing Info~"
    Exit Sub 'I thought this would do it, but obviously not
    Else
    MsgBox "'Tis Fine!",vbinformation,"Title"
    End If
    End With

    End Sub[/vba]
    Last edited by OTWarrior; 06-12-2008 at 01:42 AM. Reason: spelling in title
    -Once my PC stopped working, so I kicked it......Then it started working again

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Use DocClose.
    [vba]
    Sub DocClose()
    Dim response
    response = MsgBox("Yes or no", vbYesNo)
    If response = vbYes Then
    ActiveDocument.Close
    Else
    MsgBox "Sorry...I am not going to close the document."
    Exit Sub
    End If
    End Sub

    [/vba]Neither File > Close, OR the "X" Close button, will close the document unless you click "Yes."

    Please use the underscore character when posting code. Thanks.

  3. #3
    VBAX Mentor OTWarrior's Avatar
    Joined
    Aug 2007
    Location
    England
    Posts
    389
    Location
    Unfortunately, DocClose doesn't fire on the close event (or it doesn't on my pc, word 2003 if that helps).

    If I call your code from within document_close, it does still close not matter which option I choose

    Thanks for the help though.

    ps: sorry about the long line, I have amended it
    -Once my PC stopped working, so I kicked it......Then it started working again

  4. #4
    VBAX Mentor OTWarrior's Avatar
    Joined
    Aug 2007
    Location
    England
    Posts
    389
    Location
    I have figured out that you can stop word from closing using ctrl+break, but when I used it in the code, it doesn't always fire. it has more chance to fire if there is a "Stop" command before it.

    [VBA]Private Sub Document_Close()
    Dim response
    response = MsgBox("Yes or no", vbYesNo)
    If response = vbNo Then
    MsgBox "Sorry...I am not going to close the document."
    Else
    End If
    Stop
    SendKeys "^({BREAK})"
    End Sub[/VBA]

    Alas; this still does not fix it, but is one step closer I feel.
    -Once my PC stopped working, so I kicked it......Then it started working again

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    How interesting. It works for me. With DocClose, if I click the "X" button, OR File > Close I get the message, and if I do not click Yes, it does NOT close.

    Hmmmm.

    "If I call your code from within document_close, it does still close not matter which option I choose"

    But I do NOT call it from Document_Close. It will not work from Document_Close. That is a completely separate event. It has to be an independent Sub in a standard module.

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Just to reiterate, you are using:

    Private Sub Document_Close()

    Mine is (and I posted it as such):

    Sub DocClose()

    They are NOT the same. DocClose is the final closing routine, so it is THERE that you want your stuff.

    If you execute instructions in Document_Close, the "normal" (built-in) instructions in DocClose will still execute. You must overwrite DocClose itself.

  7. #7
    VBAX Mentor OTWarrior's Avatar
    Joined
    Aug 2007
    Location
    England
    Posts
    389
    Location
    I copied your code exactly as you posted it, and tried it in both ThisDocument and a standard module, and it will not execute.

    What am I doing wrong? Does it need to go into a particular place?
    -Once my PC stopped working, so I kicked it......Then it started working again

  8. #8
    VBAX Mentor OTWarrior's Avatar
    Joined
    Aug 2007
    Location
    England
    Posts
    389
    Location
    I just tried all the close methods, and it only fires on my computer when you close the document using the "X" menu button, not word. When it does fire from here, it works perfectly.

    Now I need to figure out how fire it when word closes, not just the document.

    Thanks for your help, I appreciate it
    Last edited by OTWarrior; 06-13-2008 at 01:25 AM. Reason: sudden revelation
    -Once my PC stopped working, so I kicked it......Then it started working again

  9. #9
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Excuse me, but you wrote:

    "only on document close"

    You never mentioned closing Word itself. Your initial code was using Document_Close...again, not Word itself.

    Hmmm, I think you will have to use WithEvents...I think. Let me check it out.



    Hmmmm...no luck so far. I made a Class, and tried using Quit to intercept closing Word itself, but I can not (so far) make it work the way you want. Tony? Steve?

  10. #10
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by fumei
    Hmmmm...no luck so far. I made a Class, and tried using Quit to intercept closing Word itself, but I can not (so far) make it work the way you want. Tony? Steve?
    Hi Gerry,

    Is it ok if it is me....? Been a long time my friend!

    Hi OT,

    The answer to your problem is making use of Word Application Events. Some of these events have a Cancel parameter.

    For your particular case you can use the DocumentBeforeClose application event.

    To use application events do the following:
    a. In your project add a new Class module.
    b. Rename the module to (for instance): oAppClass
    b. In this module paste the following code:
    [VBA]
    Option Explicit
    Public WithEvents oApp As Word.Application
    Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
    Dim response As Integer
    response = MsgBox("Close, Yes or no?", vbYesNo + vbQuestion)

    If response <> vbYes Then
    Cancel = True
    MsgBox "Sorry...I am not going to close the document."
    End If
    End Sub

    [/VBA]
    c. This class needs to be instantiated so add a new (normal) module.
    d. in this module paste the following code:
    [VBA]
    Option Explicit
    Dim oAppClass As New oAppClass
    Public Sub AutoOpen()
    Set oAppClass.oApp = Word.Application
    End Sub

    [/VBA]

    Save the lot close and reopen to test.
    I've added a sample file as attachment for you to test.

    HTH
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Of course MOS! You beat me to it. Just came up with that myself.

  12. #12
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by fumei
    Of course MOS! You beat me to it. Just came up with that myself.
    Of course you would Gerry, it was a case of specifications!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  13. #13
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Joost, about time you raised your ugly antlers around here......good to see you. Of course this couldn't be just a simple fix as "Word is different"
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  14. #14
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Steve,

    Nice to see you haven't lost your touch! (thanks for the nudge)
    Quote Originally Posted by lucas
    "Word is different"
    Just like me Steve... just like me
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  15. #15
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    b. Rename the module to (for instance): oAppClass
    This code looks great but apparently I have never had occasion to rename a Word module. Perhaps, others are abit behind as well. I'm sure it's easy but I can't seem to rename a module and trial the code... and the code doesn't work if you don't. I'm sure We would appreciate some more comments and learning if you have abit more time. Dave

  16. #16
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Dave
    This code looks great but apparently I have never had occasion to rename a Word module.
    Hi Dave,

    Sure, after you've inserted the module (menu insert):
    a. Just select that module
    b. Call the properties screen (F4)
    c. In there it has a name property you can change.

    HTH
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  17. #17
    VBAX Mentor OTWarrior's Avatar
    Joined
    Aug 2007
    Location
    England
    Posts
    389
    Location
    Wow, lots of responses...thanks guys, you have been a tremendious help.

    Sorry about the confusion Fumei, I asumed that when you close word, word will fire a "close document" type of proceedure. The Users of this document aren't very technically inclinded, so I should have been clearer with what I wanted, but thank you for the help.

    Your code worked perfectly Mos Master (I only tried adding it to my document, and it it flawless) so thanks for your time with this. I will now close the thread
    -Once my PC stopped working, so I kicked it......Then it started working again

  18. #18
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by OTWarrior
    Your code worked perfectly Mos Master (I only tried adding it to my document, and it it flawless) so thanks for your time with this. I will now close the thread
    Hi OT,

    Your welcome, glad I could help!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  19. #19

    Thank you for sharing!

    I've been beating my head on the wall for an hour trying to figure out how to do this... I've never coded anything in VB and my job needed a way to remind people to update the document properties and version history in MS Word. I knew it could be done and this definately helps get me started down the right path.

    Again, Thanks

  20. #20
    VBAX Newbie
    Joined
    Aug 2014
    Posts
    3
    Location
    Dear MOS MASTER thank you for this code. You have helped me so much !!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •