PDA

View Full Version : Intercept printing



max76
05-17-2017, 10:03 AM
Hi everyone,

Is there a way to intercept printing or saving in Word 2010? I want to trigger a macro when the user clicks on the "Save" or "Print" button?

Thank you in advance.

Massimo

gmaxey
05-17-2017, 11:32 AM
You need to create and application event class and intercept the built-in Document before save and Document before print events:


Option Explicit
Private WithEvents mWordApp As Word.Application
Private Sub Class_Initialize()
Set mWordApp = Word.Application
End Sub

Private Sub mWordApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
MsgBox "Printing"
lbl_Exit:
Exit Sub
End Sub

Private Sub mWordApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "Saving"
lbl_Exit:
Exit Sub
End Sub


You can initialize the class from a AutoExec procedure in a standard module:



Private oClsEvents As clsEvents
Sub AutoExec()
Set oClsEvents = New clsEvents
End Sub

max76
05-21-2017, 09:03 AM
Hi Greg,

thank you for your reply and code. I tried it but unfortunately when I run the AutoExec macro I get the error message:"Type defined by user not defined" and this portion of the code is highlighted Private oClsEvents As clsEvents. I attached the file with the code.


Could you please shed some light on why the code is not working correctly?

Thank you very much for your support.

Regards

Massimo

gmaxey
05-21-2017, 05:24 PM
You need to rename your Class1 to clsEvents

max76
05-22-2017, 12:18 PM
Thank you so much Greg! It's working great now! It is the very first time I have been using a class module.

Massimo

max76
08-14-2017, 10:06 AM
Hi Greg,

I've another question regarding this topic. Is there a way to prevent the printing if a certain condition (that I have created through a macro) is not met?

Thank you again for your support

Regards

Massimo

gmayor
08-15-2017, 12:32 AM
Replace the macro with

Private Sub mWordApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
If MsgBox("Print?", vbYesNo) = vbYes Then
MsgBox "Printing"
Else
MsgBox "Printing cancelled"
Cancel = True
End If
lbl_Exit:
Exit Sub
End SubThe message boxes are just to indicate what happens when the macro is run. The first message box represents a test for your 'condition'
The second demonstrates what happens when the result of that test is True, the third cancels the process when the test result is False. Without knowing about the 'condition' you want to test, it is difficult to be more precise.

max76
09-05-2017, 03:34 AM
Hi Greg,

first of all sorry for the long delay of my reply. Thank you SO MUCH for your feedback. It worked GREAT!!!!! You're definitely an excellent reference when it comes to Word macros :clap::bow:

Have a great day!

Massimo

max76
10-25-2017, 04:19 AM
Hi Greg,

I'm writing you again regarding this topic because I noticed that while the code works perfectly with Word 2010 it doesn't with Word 2016 because the dialogue box when saving the file (for the first time) is different. Is there a way to adapt the code to work on Word 2016 as well?

Thank you very much in advance for your support.

Regards

Massimo

gmaxey
10-25-2017, 06:43 AM
I just tested here an the events fire as expected. Regardless of the save dialog, DocumentBeforeSave is still the same event.

max76
10-27-2017, 10:10 AM
Hi Greg,

Thank you for your reply. However I noticed that when using Word 2016 the event is triggered only when I click on the "Save As" button on the ribbon above but not when I click on the "Save" button on the ribbon as described in the little image attached below and I don't understand why...

Awaiting your feedback.

Thank you in advance

Massimo

20784

gmaxey
10-27-2017, 02:06 PM
Massimo,

It does here, so I can't say why your are having troubles. Sorry.