Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-19-2021, 01:34 AM
Gerrit Gerrit is offline DocumentBeforePrint doesnt work Windows 10 DocumentBeforePrint doesnt work Office 2019
Novice
DocumentBeforePrint doesnt work
 
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, 20 views)
Reply With Quote
  #2  
Old 02-19-2021, 09:58 PM
gmayor's Avatar
gmayor gmayor is offline DocumentBeforePrint doesnt work Windows 10 DocumentBeforePrint doesnt work Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The code I use for the BeforePrint event is an old Word MVP code detailed at
Intercepting events like Save and Print and
Writing application event procedures
While this code has been around since Word 2000, it is still valid today and works in the same Word version that you are using.
Note that it doesn't fire until you click 'Print' on the File Print dialog
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 02-20-2021, 03:02 AM
eduzs eduzs is offline DocumentBeforePrint doesnt work Windows 10 DocumentBeforePrint doesnt work Office 2019
Expert
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

Message retracted. Code works for before print, changed implementation. Sorry.
__________________
Backup your original file before doing any modification.
Reply With Quote
  #4  
Old 02-20-2021, 04:00 AM
macropod's Avatar
macropod macropod is offline DocumentBeforePrint doesnt work Windows 10 DocumentBeforePrint doesnt work Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by eduzs View Post
I did some tests here and as I had noticed a while ago, I was unable to to intercept the document before print event with this code.
The code in Graham's link still works. If it didn't work for you, the problem was with your implementation, not the code.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 02-20-2021, 05:00 AM
eduzs eduzs is offline DocumentBeforePrint doesnt work Windows 10 DocumentBeforePrint doesnt work Office 2019
Expert
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

Code works for before print, changed implementation. Thanks. Sorry.
I found that for the code to work each time it's changes or another event monitor is included, it is necessary to close and reopen Word.
__________________
Backup your original file before doing any modification.
Reply With Quote
  #6  
Old 02-20-2021, 05:54 AM
gmayor's Avatar
gmayor gmayor is offline DocumentBeforePrint doesnt work Windows 10 DocumentBeforePrint doesnt work Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You don't have to reopen Word, you can simply manually run the AutoExec macro again.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 02-20-2021, 08:53 AM
eduzs eduzs is offline DocumentBeforePrint doesnt work Windows 10 DocumentBeforePrint doesnt work Office 2019
Expert
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

You're right. Thanks!
__________________
Backup your original file before doing any modification.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
DocumentBeforePrint doesnt work Code doesnt work properly ksigcajun Word VBA 6 04-06-2015 07:41 AM
Powerpoint save as WMV but media doesnt work. hooney PowerPoint 0 02-21-2013 09:45 PM
Safe Senders Doesnt work Anil Kaul Outlook 0 11-16-2012 01:28 PM
DocumentBeforePrint doesnt work Command Button doesnt work on network rmw85 Word VBA 1 04-25-2012 01:02 PM
DocumentBeforePrint doesnt work My Product code doesnt work!!!! PLEASE HELP dukquaknoobhack Office 1 01-05-2012 03:43 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:02 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft