BeforePrint

saxon25

New Member
Joined
Dec 13, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi just wondering if any one can help i have a beforeprint to check the form has been completed correctly before printing and a Msg box that askes if it has printed out ok.

the question i have is can i stop the msgbox showing unless the form is completed as i have a yes and no option if you click yes it saves the file to PDF and clears the form and being this pops up after showing the error before print there is a risk of someone pressing yes and resetting the form.

VBA Code:
Sub PrintScreen()
ActiveSheet.PageSetup.CenterFooter = Format(Now, "DD MMMM YYYY - HH:MM")
ActiveSheet.PrintOut

Dim AnswerYes As String
Dim AnswerNo As String

AnswerYes = MsgBox("Has the document printed out ok? - if No fix the problem and try again.", vbQuestion + vbYesNo, "Document Printing")
If Answer = vbNo Then MsgBox "Please rectify the problem and print again", vbInformation, "Not Printed Out"
 
 If AnswerYes = vbYes Then Call Clearform

End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
First, place the following code in the code module for ThisWorkbook (Visual Basic Editor >> View >> Project Explorer >> right-click ThisWorkbook >> select View Code)...

VBA Code:
Option Explicit

Dim m_allowPrinting As Boolean

Public Property Let allowPrinting(ByVal v As Boolean)
    m_allowPrinting = v
End Property

Public Property Get allowPrinting() As Boolean
    allowPrinting = m_allowPrinting
End Property

Private Sub Workbook_BeforePrint(Cancel As Boolean)
    If Not m_allowPrinting Then
        Cancel = True
        MsgBox "Please print using the macro!", vbExclamation
        Exit Sub
    End If
End Sub

Then place the following code in a regular module (Visual Basic Editor >> Insert >> Module)...

VBA Code:
Option Explicit

Sub PrintScreen()

    ThisWorkbook.allowPrinting = True

    ActiveSheet.PageSetup.CenterFooter = Format(Now, "DD MMMM YYYY - HH:MM")
    ActiveSheet.PrintOut
    
    ThisWorkbook.allowPrinting = False
    
    If MsgBox("Has the document printed out ok? - if No fix the problem and try again.", vbQuestion + vbYesNo, "Document Printing") = vbYes Then
        Call Clearform
    Else
        MsgBox "Please rectify the problem and print again", vbInformation, "Not Printed Out"
    End If

End Sub

Hope this helps!
 
Upvote 0
Hi Domenic thanks for your reply i will give this a go later on today (y)
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top