Consulting

Results 1 to 12 of 12

Thread: Intercept printing

  1. #1
    VBAX Regular
    Joined
    Apr 2017
    Posts
    34
    Location

    Question Intercept printing

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,338
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Apr 2017
    Posts
    34
    Location

    Intercept printing

    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
    Attached Images Attached Images
    Attached Files Attached Files

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,338
    Location
    You need to rename your Class1 to clsEvents
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular
    Joined
    Apr 2017
    Posts
    34
    Location
    Thank you so much Greg! It's working great now! It is the very first time I have been using a class module.

    Massimo

  6. #6
    VBAX Regular
    Joined
    Apr 2017
    Posts
    34
    Location
    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

  7. #7
    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 Sub
    The 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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  8. #8
    VBAX Regular
    Joined
    Apr 2017
    Posts
    34
    Location
    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

  9. #9
    VBAX Regular
    Joined
    Apr 2017
    Posts
    34
    Location
    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

  10. #10
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,338
    Location
    I just tested here an the events fire as expected. Regardless of the save dialog, DocumentBeforeSave is still the same event.
    Greg

    Visit my website: http://gregmaxey.com

  11. #11
    VBAX Regular
    Joined
    Apr 2017
    Posts
    34
    Location
    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

  12. #12
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,338
    Location
    Massimo,

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

    Visit my website: http://gregmaxey.com

Tags for this Thread

Posting Permissions

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