PDA

View Full Version : Solved: Running macro on word document close, stopping close



OTWarrior
06-11-2008, 08:41 AM
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:

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

fumei
06-11-2008, 09:22 AM
Use DocClose.

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

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.

OTWarrior
06-12-2008, 01:51 AM
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 :)

OTWarrior
06-12-2008, 07:25 AM
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.

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

Alas; this still does not fix it, but is one step closer I feel.

fumei
06-12-2008, 09:58 AM
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.

fumei
06-12-2008, 10:02 AM
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.

OTWarrior
06-13-2008, 12:30 AM
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?

OTWarrior
06-13-2008, 01:15 AM
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 :)

fumei
06-13-2008, 08:58 AM
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?

MOS MASTER
06-13-2008, 09:55 AM
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, :hi:

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:

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


c. This class needs to be instantiated so add a new (normal) module.
d. in this module paste the following code:

Option Explicit
Dim oAppClass As New oAppClass
Public Sub AutoOpen()
Set oAppClass.oApp = Word.Application
End Sub



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

HTH

fumei
06-13-2008, 10:21 AM
Of course MOS! You beat me to it. Just came up with that myself.

MOS MASTER
06-13-2008, 11:03 AM
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! :*)

lucas
06-13-2008, 11:25 AM
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"

MOS MASTER
06-13-2008, 11:46 AM
Hi Steve,

Nice to see you haven't lost your touch! (thanks for the nudge)

"Word is different"
Just like me Steve... just like me

Dave
06-13-2008, 11:59 PM
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

MOS MASTER
06-14-2008, 07:43 AM
This code looks great but apparently I have never had occasion to rename a Word module.
Hi Dave, :hi:

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

OTWarrior
06-16-2008, 12:19 AM
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 :)

MOS MASTER
06-16-2008, 02:08 AM
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, :hi:

Your welcome, glad I could help!

EmmytehN00b
10-13-2008, 12:18 PM
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 :)

kurzin
08-14-2014, 09:28 PM
Dear MOS MASTER thank you for this code. You have helped me so much !!!

fumei
08-16-2014, 11:12 PM
Hey Joost, has it really been six years?? Where have you been?

kurzin
08-16-2014, 11:51 PM
Hey Joost, has it really been six years?? Where have you been?

This problem fired just few days ago and i solved it by finding this post of MOS MASTER. Then i rregistered and thanked him.

fumei
08-17-2014, 11:28 AM
Huh? I do not understand your post. I was talking to Joost, who has been away from the forum for a long time.

kurzin
08-17-2014, 11:55 AM
Huh? I do not understand your post. I was talking to Joost, who has been away from the forum for a long time.

Ok. I am sorry. I am from Russia. I meant that JOOST was some foreiners slang.

fumei
08-17-2014, 04:33 PM
No problem. I should not have posted, as it had nothing to do with the thread.