Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run a word macro before printing

Status
Not open for further replies.

derren

Programmer
Mar 22, 2001
500
GB
Hi

I have a set of macros that I need to run when the user presses the print button (stripping out superfluous text, validation etc.). Dull, but efective. I am struggling to get the event handlers to work though.

I have created my class module and declared the object variable:

Public WithEvents App As Word.Application

[Oh, I have shamelessly nicked everything from the Word VBA help file in case you haven't noticed - have dog, bark self principle]

I then proceeded to add my app_documentbeforeprint() procedure to the class module (is this the right place to put it?). All this has in it at the moment is the ubiquitous messagebox and a call to cancel the print. I am following the help file well by now, and false confidence is rising ... until I hit this:

Code:
Before the procedure will run, you must connect the declared object in the class module (App in this example) with the Application object. You can do this with the following code from any module.

Dim X As New EventClassModule
Sub Register_Event_Handler()
    Set X.App = Word.Application
End Sub

Run the Register_Event_Handler procedure. After the procedure is run, the App object in the class module points to the Word Application object, and the event procedures in the class module will run when the events occur.

[ponder]! Where am I supposed to put this? I have put it into my macro module, to no avail, and how do I run the register_event_handler procedure?

Lost and in need of guidance! [fish] Derren
[Mediocre talent - spread really thin]
 
In the ThisWorkbook object (VBA Editor), use the BeforePrint event.
Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)

End Sub
Hope this helps...
-------------------------------------------------------------
Who is General Failure and why is he reading my hard disk?
 
Hi

Without wishing to sound like an ungreatful pedant; that is only available in Excel - unfortunately - and I am using Word for this particular project.

Thanks for your help anyway.

Any takers for the Word version? Anyone? Derren
[Mediocre talent - spread really thin]
 
That's probably why you indicated Word in the title of the thread!

Sorry about that!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top