# DocumentBeforePrint in Word



## Domski (Oct 17, 2007)

Hi again,

I'm wanting to set a pop up to appear before anyone prints one of our letter headings. I did a bit of searching looking for something like the BeforePrint event in Excel and came upon the DocumentBeforePrint Event.

I'm fairly novice when it comes to VBA and have not come across Class modules before but have tried to follow the instructions in the help file but am still getting an error.

This is the event code which I pretty much copied from help:


```
Public WithEvents appWord As Word.Application

Private Sub appWord_DocumentBeforePrint _
        (ByVal Doc As Document, _
        Cancel As Boolean)

    Dim intResponse As Integer

    intResponse = MsgBox("Have you chosen your " _
        & "service from the drop-down?", _
        vbYesNo)

    If intResponse = vbNo Then Cancel = True
End Sub
```

It also mentions that the class must be initialised which I thought you should do by running this (also from help) from a normal module:




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

I am however getting the following error when I try to run initialisation code:

Compile error: User-defined type not defined

Apologies if I'm being a bit of a thicky twin.

Any help would be appreciated as always.

Dom


----------



## Domski (Oct 17, 2007)

Problem solved.

I started again following the instructions here: Writing application event procedures and after a couple of false starts it works great.

Think I'm going to have to do a bit more research on class modules before I really understand what they're doing.

Dom


----------

