View Single Post
 
Old 02-19-2021, 01:34 AM
Gerrit Gerrit is offline Windows 10 Office 2019
Novice
 
Join Date: Feb 2021
Posts: 1
Gerrit is on a distinguished road
Angry DocumentBeforePrint doesnt work

I am trying to capture Ctrl-P and the Print-button to redirect.

I have searched and found the following macro:

ThisDocument section:
Option Explicit


Public Sub AutoExec()
Register_Event_Handler
End Sub

Module1:
Option Explicit
Dim X As New EventClassModule

Sub Register_Event_Handler()
Set X.WordAppEvents = Word.Application
End Sub

EventClassModule:
Option Explicit

Public WithEvents WordAppEvents As Word.Application

Private Sub WordAppEvents_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
Cancel = True
' Run code directly inside this Sub OR
MsgBox "Before Print"
' Call another Sub here, note, Sub and Module name can't match
Cancel = False

End Sub
*************************************
The template, in which this macro is, is loaded automatically when starting Word. When I try to print, I would expect the messagebox, but this is not shown.

When I modify the EventClassModule:

Private Sub WordAppEvents_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)

to

Private Sub WordAppEvents__DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)

The popup is shown.

It seems DocumentBeforePrint has a problem. I am using Word version 2101 (build 13628.20380 Click-and-Run)

Anyone who can help me get this working?

Thanks in advance for your response.

Gerrit
Attached Images
File Type: jpg DocumentBeforePrint.JPG (60.6 KB, 22 views)
Reply With Quote