views:

131

answers:

2

I'm looking to do a formatting check on word files before they get sent to the printer and it needs to be completely transparent to the user (no extra controls, just using the standard print options available through the UI). Is there an OnPrint or BeforePrint event, or something that can be used in that respect which I could attach a macro to, the same way I can with Open, Close, or Save? This feels like it should be simple... but those are famous last words.

Thanks in advance, Rob

A: 

I don't think you have an event for Print.. Instead try Application.OnKey to attach your code to standard Ctrl+P, and attach your code to the standard Print menu option and tool bar button.

Adarsha
+3  A: 

You can setup a wrapper class to enable Word's application events.

In your document, you will need to create a class module. This Class module will be called "clsEvents". Paste this code into your new class module:

Public WithEvents myApp As Word.Application

Public Sub myApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)

    'add your code here
    MsgBox "Blah"
End Sub

Next, create a standard module. This will be the sub that will load the application instance into your class. Name this standard module "Events". Then Paste this code:

Public e As clsEvents

Public Sub SetupEvents(theApp As Application)
    Set e = New clsEvents
    Set e.myApp = theApp
End Sub

Lastly, we need to call that subroutine that you just created. The easiest way to do it is to call it on the document_open event from "ThisDocument" module. Paste this code:

Private Sub Document_Open()
    SetupEvents Me.Application
End Sub

This will also allow you to use all the other Word Application events that are generally hidden without the document wrapper.

Close the application, and next time the document is opened, and the user tried to print, your code will execute.

Hope that helps!

Fink
nothing like hacking the event model apart... thanks!
notnot
Glad it worked :-)
Fink