PDA

View Full Version : Solved: Before print function



U_Shrestha
11-05-2009, 07:52 AM
Hi all,

I would like a command button to do the following when triggered:

1) Check the date in h4, if it is not 'today' then show a pop-up message saying "Are you sure you want to print timesheet without the current date?" Then another botton/option to click and trigger the print event.

2) If H4 already has the current date then simply print the spreadsheet.

Can someone please provide me the code? Thanks.

mikerickson
11-05-2009, 08:17 AM
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim flag As Boolean
With Sheet1.Range("h4")
If Not IsDate(.Value) Then
flag = True
Else
flag = (.Value <> Date)
End If
End With
If flag Then
Cancel = MsgBox("Do you really want to print", vbYesNo) = vbNo
End If
End Sub

U_Shrestha
11-05-2009, 08:34 AM
Hi mikerickson,

Thanks for the code.

What name should I give to the command button. Currently the first 2-lines of the code looks like this, so it is not working.

Private Sub CommandButton1_Click()
Private Sub Workbook_BeforePrint(Cancel As Boolean)

lucas
11-05-2009, 09:48 AM
U, You put the code Mike provided in the thisworkbook module. No button is necessary. Any way you try to print will run it.

U_Shrestha
11-05-2009, 11:30 AM
Thanks Steve for the explanation. Mike's code did work after I put it in ThisWorkbook.

Thanks again Mike.