Quick VBA cancel print question

micfly

Well-known Member
Joined
Sep 8, 2008
Messages
543
VBA Code:
Sub Button561_Click()
' Enter_Date_and_Print()
    Dim MyDate As Date
    MyDate = Application.InputBox("Please Enter a Date", "Date")
    Range("C3") = MyDate
    
    ' Change Copies:=2 (below) to 1 or however many copies you want
    ActiveWindow.SelectedSheets.PrintOut Copies:=2

End Sub

If the user clicks Cancel instead of entering a date it still prints. What can I add to this statement to cancel the print job if they click Cancel instead of OK?
Also, is there a way to get a prompt for which Printer to use?
 
Code:
Application.CommandBars.ExecuteMso "PrintPreviewAndPrint"
HTH. Dave

Thanks, Dave - that worked!

I replaced: ActiveWindow.SelectedSheets.PrintOut Copies:=2
with: Application.CommandBars.ExecuteMso "PrintPreviewAndPrint"

Now it all works and I can select which printer.
VBA Code:
Sub Button561_Click()
' Enter_Date_and_Print()
    Dim MyDate As Date
    Dim Str As String
 
    Str = Application.InputBox("Please Enter a Date", "Date")
 
        Select Case Str
            Case False
                MsgBox "Error: Print Cancelled!"
                Exit Sub
            Case ""
                MsgBox "Error: empty input"
                Exit Sub
            Case Is <> ""
                If IsDate(Str) = True Then
                    MyDate = Str
                    Range("C3") = MyDate
                    
                'Change Copies:=2 (below) to 1 or however many copies you want (uncomment for below)
                'ActiveWindow.SelectedSheets.PrintOut Copies:=2
                
                'Use this one (below - instead of above) to select which printer to use before printing
                Application.CommandBars.ExecuteMso "PrintPreviewAndPrint"
                
                Else
                    MsgBox "invalid date"
                End If
            Case Else
                MsgBox "Error: unknown error"
                Exit Sub
        End Select
End Sub
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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