PDA

View Full Version : Solved: Intercepting Events



petedw
05-09-2005, 02:24 AM
Hi,

Could someone please write me a macro that intercepts the print command.
I am using forms and would like it to cancel the print if the form is not locked and also include pop up a message telling the user to lock the form before attempting to print again.

I hope you can help me with this

Many Thanks

Pete

TonyJollans
05-09-2005, 03:35 AM
Hi Pete,

A slightly unusual way of working with Forms. Would you care to say why you want to do this?

To intercept printing via the File menu (and ctrl+p which does the same thing) requires a macro called FilePrint.

Sub FilePrint()
If ActiveDocument.protectiontype = wdAllowOnlyFormFields Then
Application.Dialogs(wdDialogFilePrint).Show
Else
Msgbox "Protect or Die!!"
end If
end sub

You will also need to redirect the print buton on the standard toolbar, with something like this (in the document_open perhaps)

Commandbars.FindControl(id:=2521).OnAction = "FilePrint"

MOS MASTER
05-09-2005, 06:09 AM
You will also need to redirect the print buton on the standard toolbar, with something like this (in the document_open perhaps)

Commandbars.FindControl(id:=2521).OnAction = "FilePrint"
Hi Tony, :D

That's nice lateral thinking of you but the print button also has it's own Word macro called: "FilePrintDefault"

Hence the macro:
Sub FilePrintDefault()
'Intercept printmenu button
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.PrintOut
Else
MsgBox "Protect or Die!!"
End If
End Sub


So Pete should use both the FilePrint & FilePrintDefault to intercept the printbuttons.

Enjoy! :whistle:

petedw
05-09-2005, 06:51 AM
Could you please let me know how to re-direct the Close and Close Window buttons too? (in the document_open)

Thanks again

Pete

MOS MASTER
05-09-2005, 06:56 AM
Could you please let me know how to re-direct the Close and Close Window buttons too? (in the document_open)

Thanks again

Pete
Hi, :D

You're welcome!

Don't understand your question.

You want to intercept close buttons in the document_open eventhandler? I have no clue what your getting at.

And could you be more specific about wich close buttons you wish to intercept because there are a lot off ways that you can close. So please say something like:

I Choose File/Close (So you want that event)

Enjoy! :whistle:

petedw
05-09-2005, 06:59 AM
Im refering to the close and close windows buttons that appear in the top right hand corner of word.

I hope you can help me with this

Thank

Pete

MOS MASTER
05-09-2005, 07:03 AM
Hi Pete, :D

Ok I'll answer that question in a bit. Have to prepare dinner now..:rofl:

fumei
05-09-2005, 07:14 AM
Hi Pete,

Could you please elaborate on what you are trying to do...and why?

"Re-direct" the Close button. Redirect where, and why?

petedw
05-09-2005, 07:20 AM
Gerry,

I am trying to redirect the 'close' and 'close windows' buttons that appear in the top right hand corner of word to 'file-exit'.
The reason for this is that i have a macro set up to run once file-exit is executed.

Hope you understand what i mean

Pete

MOS MASTER
05-09-2005, 07:45 AM
Gerry,

I am trying to redirect the 'close' and 'close windows' buttons that appear in the top right hand corner of word to 'file-exit'.
The reason for this is that i have a macro set up to run once file-exit is executed.

Hope you understand what i mean

Pete
Hi Pete,

Just use this sub:
Sub AutoClose()
MsgBox "Closing now"
End Sub


Instead of the msgbox you call your sub in it like:
Call MyCloseSub

But remember this is just a redirect so your function should make sure everything is done that you want it to do.

So if you want to protect the document do that.
If you want to print it do that.

But make sure the last thing you do is Save the document: (if you don't do that Word will ask what you want to do!)

Activedocument.Save


And Leave the closing part to the AutoClose sub!! That is the event that happens last.

If you need help programming your custom close sub than place it over here.

Enjoy! :whistle:

fumei
05-09-2005, 08:09 AM
As Joost suggests - use AutoClose. What you want is a something to fire your Sub when the document is closed, correct? There is no need - at least as far as you have stated - to use a redirect of the close button. If there is an AutoClose Sub, it will fire.

HOWEVER, and this is an important point, the document WILL CLOSE. You are adding other things to the closing procedure, but not stopping it. So if you want to have logic that states:

IF condition A exists...then sure, OK, allow the file to close, but IF condition B exists...then, no way, nope, this file is NOT going to be closed.

then that is another story.

Which is why I asked - What are you trying to do? It was not a trivial question. It is very helpful to fully state what the situation is. Unfortunately very few have actually worked out what they really want to do. Or can be clear about it.

You say you want to run a macro on file-exit. Well that is OK, but:

Does this macro have consequences on the file that is closing?
Does this macro require logic like above, that is, something that will STOP the file closing?

Anyway, as you did not actually say what you are trying to do, unless you really need to, use AutoClose.

Good luck.

MOS MASTER
05-09-2005, 08:15 AM
Hi Gerry, :D


And Leave the closing part to the AutoClose sub!! That is the event that happens last.


This is infact the question over here and like you I'm only able to answer to the information that's given.

Yes this sub will close and there's no stopping this AutoClose sub. (This is not news for you I'm sure)

If the Op wishes to have a cancel parameter we must go to Application events for sollutions...(perhaps other have to think that one over)

Lets wait for Pete to join and explain...:whistle:

petedw
05-09-2005, 08:24 AM
The macro that i will be calling with the AutoClose is as follows;

Sub FileExit()
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
WordBasic.FileExit
Else
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True
WordBasic.FileExit
End If
End Sub


This all runs exactly how i want it to accept for one thing. When i trigger

Sub AutoClose()
Call FileExit
End Sub

i receive a warning message from Word "You cannot close Microsoft Word because a dialog is active. Switch to Microsoft Word first and close the dialog." This is then followed by the box i want to appear "Save? - yes, no, cancel".
Is there any way of getting rid of warning message but keeping the save prompt message??

HELP!!!!

Pete

MOS MASTER
05-09-2005, 08:28 AM
Hi Gerry, :D

I was wrong in my assumption that you can't cancel the AutoClose sub!

There is a dirty way:
Sub AutoClose()
ActiveDocument.Saved = False
SendKeys "{ESC}"
End Sub


I will take a look at your reply now Pete! :whistle:

MOS MASTER
05-09-2005, 08:36 AM
Hi Pete, :D

Just change your sub to:
Sub AutoClose()
With ActiveDocument
If .ProtectionType <> wdAllowOnlyFormFields Then
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End With
End Sub


This will leave you with a protected document in the end and you will receive the question for Word if you want to save the document.

If you want to make sure that the document is protected for sure when it's closed you use:
Sub AutoClose()
With ActiveDocument
If .ProtectionType <> wdAllowOnlyFormFields Then
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
.Save
End If
End With
End Sub


Enjoy! :whistle:

petedw
05-09-2005, 08:41 AM
Thank You! Thank You! Thank You!

That worked fine

Pete

MOS MASTER
05-09-2005, 08:43 AM
You're Welcome! :beerchug:

fumei
05-09-2005, 08:48 AM
Darn SendKeys...yes of course.

So Pete...there you go. If you had simply stated you wanted to make sure the document was protected for forms before closing, then that is what we could have answered.

That being said...sorry, but it does not stop there.

Are you protecting for forms because you want to lock editing?
Are you protecting for forms because you actually have formfields?
If you have formfields, why is the protection OFF? FormFields do not work (not strictly true...) unless the section they are in IS protected. Who turned it off?

Why do you need to make sure protection is on?

What are you trying to do? I am not trying to be annoying. This is how it really works. This may have nothing to do with intercepting events at all. I don't know.

better specs...better code.

MOS MASTER
05-09-2005, 08:53 AM
Darn SendKeys...yes of course.
I feld the same way! :banghead: (I had to double check it, otherwise you would have kicked my but...:rofl: )

For the rest of your story:
Your just plain right...but as I mentioned before...the monkey didn't get out until the last reply




better specs...better code.
Amen. :whistle:

petedw
05-10-2005, 03:14 AM
You will also need to redirect the print buton on the standard toolbar, with something like this (in the document_open perhaps)





VBA:

Commandbars.FindControl(id:=2521).OnAction = "FilePrint"







Since i have done this, everytime i click on the print button in word, i get an error message saying "The macro cannot be found or has been disabled because of your Macro security settings." I thought redirecting the print button would only take effect in the template where i set it up?!?!

HELP ME!

Thanks

Pete

TonyJollans
05-10-2005, 05:20 AM
Hi Pete,

It is an application setting. You need to set it when opening, and unset it when closing, your document or template. (You might also want to set it when changing documents but that's another story).

I would suggest you set the OnAction to "" and then take Joost's advice and use the FilePrintDefault routine instead. This, too, will be application-wide but as it involves no setting anything it will only be in effect when it is available to Word (i.e. when your template is open).

MOS MASTER
05-10-2005, 10:05 AM
Hi Pete, :D

Tony's right off course but have you tried yet to use the 2 subs together like:
Sub FilePrintDefault()
'Intercept printmenu button
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.PrintOut
Else
MsgBox "Protect or Die!!"
End If
End Sub

Sub FilePrint()
'Intercept File Print / CTRL+P
If ActiveDocument.protectiontype = wdAllowOnlyFormFields Then
Application.Dialogs(wdDialogFilePrint).Show
Else
Msgbox "Protect or Die!!"
End If
End Sub
I have this running in a lot of templates of mine and it works brilliantly for me...

If you need help please tell! :whistle: