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
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
You need to create and application event class and intercept the built-in Document before save and Document before print events:
You can initialize the class from a AutoExec procedure in a standard module: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
Private oClsEvents As clsEvents Sub AutoExec() Set oClsEvents = New clsEvents End Sub
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
You need to rename your Class1 to clsEvents
Thank you so much Greg! It's working great now! It is the very first time I have been using a class module.
Massimo
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
Replace the macro with
The message boxes are just to indicate what happens when the macro is run. The first message box represents a test for your 'condition'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 Sub
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.
Graham Mayor - MS MVP (Word) 2002-2019
Visit my web site for more programming tips and ready made processes
http://www.gmayor.com
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
Have a great day!
Massimo
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
I just tested here an the events fire as expected. Regardless of the save dialog, DocumentBeforeSave is still the same event.
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
Save_buttons.png
Massimo,
It does here, so I can't say why your are having troubles. Sorry.