PDA

View Full Version : Solved: Need errorhandling for "print sheet" buttons



Gingertrees
11-11-2009, 09:45 AM
Hi all,
For any sheets in my workbook over 1 page long, I have "print this page" command buttons. The code looks like this:
Private Sub CommandButton11_Click()
'print Greivance pg
'initial range tells it to just print this page; after printout resets to entire
'workbook (pgs 1-21)
Sheet11.PageSetup.PrintArea = "$A$704:$AM$757"
Application.Dialogs(xlDialogPrinterSetup).Show
'lets user select printer; needed as users use different workstations often
Sheet11.PrintOut
Sheet11.PageSetup.PrintArea = "$A$1:$AM$1114"
End Sub
I added the dialogs because if the user selected "cancel" for page setup (like if it tried to print to file or XPS doc.writer), I get a runtime error 1004 "Application-defined or object-defined error". Now if they hit cancel it still tries to print, and can still get to the Runtime error if they try hard enough. I feel like this could be fixed with an If OK then sheet11.printout
If Cancel then Sheet11.PageSetup.PrintArea = "$A$1:$AM$1114"
Exit Sub
End Sub
but the if thens just do nothing. I realize I need some Errorhandling but I'm not skilled at that. Help?

lucas
11-11-2009, 10:18 AM
I added the dialogs because if the user selected "cancel" for page setup

I don't understand the steps that are throwing an error.

Gingertrees
11-11-2009, 12:32 PM
In the beginning, the code for the command button was just this:

Sheet1.PageSetup.PrintArea = "$A$1:$AG$52"
Sheet1.PrintOut
Sheet1.PageSetup.PrintArea = "$A$1:$AG$104"

And this triggered the runtime error if the printer was set to XPS Document Writer and the user hit "cancel" when s/he was asked for file location.

So I added the Application.Dialogs(xlDialogPrinterSetup).Show
'lets user select printer; needed as users use different workstations

So user could select the printer of choice. But it could STILL error if the user picks XPS Document Writer and hits Cancel (cancels the print job).
I've attached an example that you can see this on.

lucas
11-11-2009, 02:47 PM
This seems to be one of the rare times when the code is not too extensive and we know exactly what is going on and I would simply use:
On Error Resume Next

Like this:

Private Sub CommandButton1_Click()
Sheet1.PageSetup.PrintArea = "$A$1:$AG$52"
Application.Dialogs(xlDialogPrinterSetup).Show
'lets user select printer; needed as users use different workstations often
On Error Resume Next
Sheet1.PrintOut
Sheet1.PageSetup.PrintArea = "$A$1:$AG$104"
End Sub

Gingertrees
11-11-2009, 03:59 PM
Hmm...that took out the runtime error, but I'd still like the user to be able to excape from printing if s/he truly did hit it accidentally - with Resume Next it still prints to the previously-set default printer (which could be on another floor for example). I even tried moving the Resume code below Printout - same result.
Help?

lucas
11-11-2009, 08:36 PM
Ok, untested but maybe on error just go to exits which skips the print line:

Private Sub CommandButton1_Click()
Sheet1.PageSetup.PrintArea = "$A$1:$AG$52"
Application.Dialogs(xlDialogPrinterSetup).Show
'lets user select printer; needed as users use different workstations often
On Error GoTo exits
Sheet1.PrintOut
exits:
Sheet1.PageSetup.PrintArea = "$A$1:$AG$104"
End Sub

Gingertrees
11-12-2009, 07:23 AM
Perfecto! Thanks.