Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-08-2016, 07:33 PM
wlcdo2 wlcdo2 is offline DocumentBeforePrint for One Document Only Windows 7 32bit DocumentBeforePrint for One Document Only Office 2013
Novice
DocumentBeforePrint for One Document Only
 
Join Date: Jun 2016
Posts: 17
wlcdo2 is on a distinguished road
Default DocumentBeforePrint for One Document Only

I've created a document using Word 2013 that contains a number of ActiveX Check Boxes. When the user attempts to print the document, I want to use DocumentBeforePrint to check the status of the Check Boxes and if the value of one or more is False, then a message will display.

I can't seem to get it to work though. I've seen an example in an older post (~2011) but it required creating a document template, then loading the template as an Add-In. That example would then activate before any and every print option - I want this to work only with a single document though.



Thanks in advance for any assistance.
Reply With Quote
  #2  
Old 06-08-2016, 09:01 PM
gmayor's Avatar
gmayor gmayor is offline DocumentBeforePrint for One Document Only Windows 10 DocumentBeforePrint for One Document Only Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

This is relatively straightforward. Add a class module to the document call it EventClassModule
In that module put the following example code. Edit it to reflect your check boxes and the message you wish to convey
Code:
Option Explicit
Public WithEvents appWord As Word.Application

Private Sub appWord_DocumentBeforePrint _
        (ByVal Doc As Document, _
         Cancel As Boolean)
Dim intResponse As Integer
Dim sBox As String

    Select Case False
        Case ThisDocument.CheckBox1.Value: sBox = "CheckBox 1"
        Case ThisDocument.CheckBox2.Value: sBox = "CheckBox 2"
        Case ThisDocument.CheckBox3.Value: sBox = "CheckBox 3"
    End Select


    intResponse = MsgBox(sBox & " has not been checked!", _
                         vbOKCancel)
    If intResponse = vbCancel Then Cancel = True
End Sub
Add an ordinary module to the document and add the following code
Code:
Option Explicit
Dim X As New EventClassModule
Sub AutoOpen()
    Register_Event_Handler
End Sub
Sub Register_Event_Handler()
    Set X.appWord = Word.Application
End Sub
Save the document as a macro enabled document. When the document is opened the autoopen macro runs and sets the event handler. (You can run it from the module for testing).
__________________
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 06-09-2016, 02:02 PM
wlcdo2 wlcdo2 is offline DocumentBeforePrint for One Document Only Windows 7 32bit DocumentBeforePrint for One Document Only Office 2013
Novice
DocumentBeforePrint for One Document Only
 
Join Date: Jun 2016
Posts: 17
wlcdo2 is on a distinguished road
Default

Thank you gmayor. Couple of points to clarify please.
1). It seems to trigger only once though. i.e. Document is opened, Print is selected and my message appears to warn the user that they should review the document and make sure the appropriate CheckBox is ticked. When the Print option is selected a successive time, the DocumentBeforePrint doesn't seem to trigger again.
2). Sorry but I didn't specify this earlier; if one or more of the CheckBox values is false, as well as displaying a message, I want the print request to cancel in order to force the user to review the document and make sure the requisite CheckBoxes are ticked.
Are both of these possible?
Regards Corin.
Reply With Quote
  #4  
Old 06-10-2016, 12:00 AM
gmayor's Avatar
gmayor gmayor is offline DocumentBeforePrint for One Document Only Windows 10 DocumentBeforePrint for One Document Only Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

The macro should prompt when any of the listed check boxes is unchecked. If all the listed check boxes must be checked before printing then instead of giving the user a choice, the warning can simply cancel the print process. e.g. change the main code to the following.

I don't get the failure to trigger the prompt on successive attempts at printing, with this code, assuming one of more of the named check boxes is unchecked. If they are all checked the document is printed.

Code:
Option Explicit
Public WithEvents appWord As Word.Application

Private Sub appWord_DocumentBeforePrint _
        (ByVal Doc As Document, _
         Cancel As Boolean)
Dim intResponse As Integer
Dim sBox As String: sBox = ""
    Select Case False
        Case ThisDocument.CheckBox1.Value: sBox = "CheckBox 1"
        Case ThisDocument.CheckBox2.Value: sBox = "CheckBox 2"
        Case ThisDocument.CheckBox3.Value: sBox = "CheckBox 3"
    End Select
    If Not sBox = "" Then
        intResponse = MsgBox(sBox & " has not been checked!", vbCritical)
        If intResponse = vbOK Then Cancel = True
    End If
lbl_Exit:
    Exit Sub
End Sub
Note you can use more meaningful check box names and/or messages associates with each box.
__________________
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
  #5  
Old 06-12-2016, 08:54 PM
wlcdo2 wlcdo2 is offline DocumentBeforePrint for One Document Only Windows 7 32bit DocumentBeforePrint for One Document Only Office 2013
Novice
DocumentBeforePrint for One Document Only
 
Join Date: Jun 2016
Posts: 17
wlcdo2 is on a distinguished road
Default

Perfect! Thanks so much. I found the issue I had with the successive running of the script was down to my work PC (old) wasn't set to update Office automatically. It was still on version 15.0.4420.1017 (03-2015). I've since upgraded, reloaded the VBA code to my Word project and it works perfectly. Thanks again so much for your assistance.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
DocumentBeforePrint for One Document Only split word document based on bookmarks with each new document title of the bookmark megatronixs Word VBA 9 09-05-2020 02:29 PM
Adding a link into a word document that when pressed, takes user to a page within the same document yan89 Word 1 04-29-2016 01:54 PM
Vba code to save document as pdf using document property text and rename folder. staicumihai Word VBA 1 12-21-2015 07:39 AM
DocumentBeforePrint for One Document Only Run-time error '1004': Document not saved. The document may be open... doctor_who12 Excel Programming 1 01-22-2014 04:47 PM
DocumentBeforePrint not working maruapo Drawing and Graphics 0 06-15-2010 12:31 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:51 PM.


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