VBA Code to Select "Cancel" when "Printer Setup" dialog box appears

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
585
Office Version
  1. 365
Platform
  1. Windows
Can anyone tell me how I would get the "Cancel" button to select "Cancel" when the "Printer Setup" dialog box appears? I'm asking because I only want the "Print" window that has all the options for the Margins, Orientation and so on to appear.

When I use the following:
VBA Code:
Application.Dialogs(xlDialogPrinterSetup).Show

A "Printer Setup" dialog box appears. Then I have to select "Cancel" in order to be taken to the "Print" screen next. Not sure if there is another way to bypass that "Printer Setup" dialog box or not.

Also, I want my code to set the print area to be the "Print Active Sheets" option.


Thank you, SS
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this:

VBA Code:
Sub test()
  If Application.Dialogs(xlDialogPrinterSetup).Show = True Then
    MsgBox "You selected: " & Application.ActivePrinter
  Else
    MsgBox "You pressed cancel"
  End If
End Sub

Or maybe you need this:

VBA Code:
Sub Macro1()
  If Application.Dialogs(xlDialogPageSetup).Show = True Then
    '
    'here what you need to do
    '
  Else
    MsgBox "You pressed cancel"
  End If
End Sub
 
Last edited:
Upvote 0
Try this:

VBA Code:
Sub test()
  If Application.Dialogs(xlDialogPrinterSetup).Show = True Then
    MsgBox "You selected: " & Application.ActivePrinter
  Else
    MsgBox "You pressed cancel"
  End If
End Sub
I don't think that is what I need. I did find the following code that gets me to the "Print" window that shows the settings and the preview at the same time.

VBA Code:
Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")

Application.SendKeys "{TAB}{TAB}{ENTER}", True

However, what I'm really after is to get the first Setting in this window to go to the "Print Active Sheets" option, versus "Print Selection" or "Print Entire Workbook". If I try and use
VBA Code:
.printout
anywhere in the code it sends what I'm printing to the printer. I just want to change the setting to the "Print Active Sheets" option until I'm ready to print.
 
Upvote 0
anywhere in the code it sends what I'm printing to the printer. I just want to change the setting to the "Print Active Sheets" option until I'm ready to print.
Try:

VBA Code:
Sub Macro1()
  If Application.Dialogs(xlDialogPageSetup).Show = True Then
    '
    ActiveSheet.PrintOut
    '
  Else
    MsgBox "You pressed cancel"
  End If
End Sub
 
Upvote 0
However, what I'm really after is to get the first Setting in this window to go to the "Print Active Sheets" option, versus "Print Selection" or "Print Entire Workbook". If I try and use


Sorry, I think you need this:

VBA Code:
Sub macro2()
  Application.Dialogs(xlDialogPrint).Show
End Sub
 
Upvote 0
Try:

VBA Code:
Sub Macro1()
  If Application.Dialogs(xlDialogPageSetup).Show = True Then
    '
    ActiveSheet.PrintOut
    '
  Else
    MsgBox "You pressed cancel"
  End If
End Sub

I'm trying to get the "Print Selection" option in the image below to automatically get reset to "Print Active Sheets". If I could do this without evening opening any different windows and just running the macro to do everything that would be great. I have some code that changes margins and orientation, but need to get that setting switched so when I need to print it is already set up for me. Sorry for the confusion. Thanks, SS
 

Attachments

  • Print Page Help.jpg
    Print Page Help.jpg
    69.3 KB · Views: 39
Upvote 0
I'm trying to get the "Print Selection" option in the image below to automatically get reset to "Print Active Sheets". If I could do this without evening opening any different windows and just running the macro to do everything that would be great. I have some code that changes margins and orientation, but need to get that setting switched so when I need to print it is already set up for me. Sorry for the confusion. Thanks, SS

It seems to me that what you want I put in post #5, I went ahead before you reviewed it, but here it goes again:
Sorry, I think you need this:
VBA Code:
Sub macro2()
  Application.Dialogs(xlDialogPrint).Show
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0
It seems to me that what you want I put in post #5, I went ahead before you reviewed it, but here it goes again:

VBA Code:
Sub macro2()
  Application.Dialogs(xlDialogPrint).Show
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
I did try that one out. That code does default to the "Print Active Sheets" setting, but when I hit "OK" it will print. If I hit "Cancel", the setting doesn't stick. I just want this code to change the setting to "Print Active Sheets" to set the file up that way for anyone using it going forward for this particular print situation.
 
Upvote 0
I'm trying to get the "Print Selection" option in the image below to automatically get reset to "Print Active Sheets"
I just want this code to change the setting to "Print Active Sheets" to set the file up that way for anyone using it going forward for this particular print situation.
Yes, I'm already confused.


If you only want to print the selected. You don't need to open any dialog.
Just this:
VBA Code:
Sub Macro3()
    Selection.PrintOut
End Sub

I hope that's what you want.
;)
 
Upvote 0
Yes, I'm already confused.


If you only want to print the selected. You don't need to open any dialog.
Just this:
VBA Code:
Sub Macro3()
    Selection.PrintOut
End Sub

I hope that's what you want.
;)
That's the issue in a nutshell. I don't want to print anything. I just want to change the setting you see in that image file I attached to "Print Active Sheets" and save the file right there. I'm adding this step to code that I already have that will set margins, orientation and everything else.

Basically I need this:
Print Selection.jpg






To change to this:
Print Active Sheets.jpg
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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