VBA warning to print 1 page

excellence

Board Regular
Joined
Oct 5, 2005
Messages
155
Office Version
  1. 365
Platform
  1. MacOS
I have a 2 page spreadsheet that I would like to warn me, prior to printing to print 1 page as I need to take the printed page and flip it to print page 2

I tried the vba below, but I can't get past the warning to print page 1 as the message keeps coming back.
Perhaps there is a different way but wonder how to edit this to answer no and proceed to the print dialog box so I can choose which page to print, not both.

My guess is to have the cancel=true modified to take me to the print dialog box?

Many thanks

VBA Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Result = MsgBox("PRINT 1 PAGE  ANSWER >>> NO", vbYesNo)
    If Result = vbNo Then
        Cancel = True
    End If
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I would write a script that prints the 1st page only with something like this:

VBA Code:
MsgBox ("This script will print page 1 only.")
ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=1, Collate:=False

Then wait for say 20-30 seconds and have a message box appear that asks "Are you ready to print page 2?" (vbYesNo).

VBA Code:
If vbYes
     ActiveWindow.SelectedSheets.PrintOut From:=2, To:=2, Copies:=1, Collate:=False
End If
If vbNo
     Exit Sub
End If
 
Last edited:
Upvote 0
Hi @excellence.
Maybe like:
Code:
Option Explicit

Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Static showMessage As Boolean
    Dim Result      As VbMsgBoxResult

    If Not showMessage Then
        Result = MsgBox("Print only one page at a time. Click 'No' to open the print dialog box and manually select the page to print.", vbYesNo)
        
        If Result = vbNo Then
            Cancel = True
            showMessage = True
            Application.Dialogs(xlDialogPrint).Show
        End If
    
    Else
        showMessage = False
    End If

End Sub
I'm not sure it will work the way you want, but it's worth a try. Let me know if this code works correctly. Good luck.
 
Upvote 0
I would write a script that prints the 1st page only with something like this:

VBA Code:
MsgBox ("This script will print page 1 only.")
ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=1, Collate:=False

Then wait for say 20-30 seconds and have a message box appear that asks "Are you ready to print page 2?" (vbYesNo).

VBA Code:
If vbYes
     ActiveWindow.SelectedSheets.PrintOut From:=2, To:=2, Copies:=1, Collate:=False
End If
If vbNo
     Exit Sub
End If
Not sure where to put the Msg Box VBA as I have another VBA that is an Auto open VBA and had put my VBA under it in "this workbook"
It seems mine works if I answer yes to the msg box, but yours seems better :)
 
Upvote 0
I would write a script that prints the 1st page only with something like this:

VBA Code:
MsgBox ("This script will print page 1 only.")
ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=1, Collate:=False

Then wait for say 20-30 seconds and have a message box appear that asks "Are you ready to print page 2?" (vbYesNo).

VBA Code:
If vbYes
     ActiveWindow.SelectedSheets.PrintOut From:=2, To:=2, Copies:=1, Collate:=False
End If
If vbNo
     Exit Sub
End If
Not sure where to put the Msg Box VBA as I have another VBA that is an Auto open VBA and had put my VBA under it in "this workbook"
It seems mine works if I answer yes to the msg box, but yours seems better :)
also don't want to print just get to print dialog.
 
Upvote 0
Hi @excellence.
Maybe like:
Code:
Option Explicit

Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Static showMessage As Boolean
    Dim Result      As VbMsgBoxResult

    If Not showMessage Then
        Result = MsgBox("Print only one page at a time. Click 'No' to open the print dialog box and manually select the page to print.", vbYesNo)
       
        If Result = vbNo Then
            Cancel = True
            showMessage = True
            Application.Dialogs(xlDialogPrint).Show
        End If
   
    Else
        showMessage = False
    End If

End Sub
I'm not sure it will work the way you want, but it's worth a try. Let me know if this code works correctly. Good luck.
An amateur here, and don't really know how/where to paste this into vba though have checked require variable declaration in preferences
 
Upvote 0
When the message pops up, walk over to the printer and flip your sheet.
Click on OK on the message box to finish printing.
Code:
Sub Maybe()
Dim i As Long, sh1 As Worksheet
Set sh1 = Worksheets("Sheet1")    '<-----Change as required
sh1.PageSetup.PrintArea = "A1:I90"    '<-----Change as required
    For i = 1 To 2
        sh1.PrintOut i, i
        If i = 1 Then MsgBox "Waiting for operator to turn paper."
    Next i
End Sub
Put the code in a regular module.
 
Upvote 0
When the message pops up, walk over to the printer and flip your sheet.
Click on OK on the message box to finish printing.
Code:
Sub Maybe()
Dim i As Long, sh1 As Worksheet
Set sh1 = Worksheets("Sheet1")    '<-----Change as required
sh1.PageSetup.PrintArea = "A1:I90"    '<-----Change as required
    For i = 1 To 2
        sh1.PrintOut i, i
        If i = 1 Then MsgBox "Waiting for operator to turn paper."
    Next i
End Sub
Put the code in a regular module.
I don't want to print necessarily, but just be warned not to print lest I print 2 pages. So the message is a warning to print just one page of the 2 page document so I would flip it IF I decided to print
 
Upvote 0
I don't get the logic. Why use "Print" if you don't want to print? Why have a message box for this?
 
Upvote 0
I don't get the logic. Why use "Print" if you don't want to print? Why have a message box for this?
Want to be reminded to select one page when printing if I print. May want to print preview the spreadsheet and not print it when I see the preview, but was reminded about just printing one page if I decide to print.
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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