Message box over print preview

cshetty

Board Regular
Joined
Apr 15, 2017
Messages
87
Office Version
  1. 2016
Platform
  1. Windows
Dear Experts

I have a VBA to preview before printing.
My requirement is to display the message box over the print preview not after closing the preview.

When I select Yes, preview screen should disappear and Print
When I select No, preview screen should disappear and go to the selected sheet.

Thank you in advance.
VBA Code:
Sub PrintChq()
Dim ans As Boolean
Dim answer As Integer
'
' PrintChq Macro

    Dim strOldPrinter As String

    strOldPrinter = Application.ActivePrinter
    Application.ScreenUpdating = False
    Sheets("Sheet2").Select
    ActiveSheet.PrintPreview
    answer = MsgBox("Yes / No", vbQuestion + vbYesNo + vbDefaultButton2, "Continue Printing Cheque ?")
    If answer = vbYes Then
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, ActivePrinter:="HP LaserJet Professional P1102"
        Application.ActivePrinter = strOldPrinter
        Sheets("Sheet1").Select
        Application.ScreenUpdating = True
    Else
        MsgBox "Print Cheque Cancelled"
    End If
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You don't say what is happening, but it seems counterintuitive to turn off screen updating when you're wanting to see the effects of the code. Perhaps see what happens when you don't do that?
 
Upvote 0
You don't say what is happening, but it seems counterintuitive to turn off screen updating when you're wanting to see the effects of the code. Perhaps see what happens when you don't do that?
Thank you, Micron, for the reply.

When the macro runs, i get the print preview screen. Once i close the preview screen i get the message box displayed for confirmation. If Yes is selected, it prints and if No is selected it aborts printing. Is there any way I get the message box on the print preview screen? I have tried with enabling screen update, but no avail. What exactly I want is When selected Yes, the preview screen should disappear and print. Same in the case of selecting No, preview screen should disappear and abort printing.

There is one more issue, Once the preview screen appears, if i press Esc button, macro gets stuck, and i have to end task.

Hope I was able to explain the issue properly.

Thanks in advance for any resolution.
 
Upvote 0
Displaying the message box will prevent you from interacting with the PrintPreview Ribbon commands because the message box is a modal window.
 
Upvote 0
Why can't you use the the Print and Close Print Preview Ribbon buttons ?
I get your point.. When I personally do close the preview screen. But I have problem with other users, who are not familiar with this, they tend to use Esc button and macro gets stuck forcing to End the task. I tried disabling the Esc option. Still it gives me problem sometimes.

VBA Code:
Sub PrintChq()
' Dim ans As Boolean
Dim answer As Integer
'
' PrintChq Macro

    Dim strOldPrinter As String
    Application.EnableCancelKey = xlDisabled
    strOldPrinter = Application.ActivePrinter
    ' Application.ScreenUpdating = False
    Sheets("Sheet2").Select
    ActiveSheet.PrintPreview
    answer = MsgBox("Yes / No", vbQuestion + vbYesNo + vbDefaultButton2, "Continue Printing Cheque ?")
    If answer = vbYes Then
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, ActivePrinter:="HP LaserJet Professional P1102"
        Application.ActivePrinter = strOldPrinter
        Sheets("Sheet1").Select
        ' Application.ScreenUpdating = True
    Else
        MsgBox "Print Cheque Cancelled"
    End If
    Application.EnableCancelKey = xlInterupt
End Sub
 
Upvote 0
I see.
Application.EnableCancelKey = xlDisabled won't work . Just tried Application.OnKey "{ESC}", "" and it doesn't work either.
Let me try something else and see what happens .
 
Upvote 0
Try this custom DisableEscapeKey Property . This should prevent users from using the ESC key while the PrintPreview window is on display. No message box required.

The issue now is that this doesn't allow you to select the number of copies or Printer name. But then, why would you need that if you are already giving the user the choice to select the required printing settings from the PrintPreview window.

VBA Code:
Option Explicit

#If VBA7 Then
    Declare PtrSafe Function RegisterHotKey Lib "user32" (ByVal hwnd As LongPtr, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    Declare PtrSafe Function UnregisterHotKey Lib "user32" (ByVal hwnd As LongPtr, ByVal id As Long) As Long
#Else
    Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
#End If
 
Sub PrintChq()
    Sheets("Sheet2").Select
    DisableEscapeKey = True
    ActiveSheet.PrintPreview
    DisableEscapeKey = False
End Sub

Private Property Let DisableEscapeKey(ByVal bDisable As Boolean)
    If bDisable Then
        Call RegisterHotKey(Application.hwnd, 1&, 0&, VBA.vbKeyEscape)
    Else
        Call UnregisterHotKey(Application.hwnd, 1&)
    End If
End Property
 
Last edited:
Upvote 0
Try this custom DisableEscapeKey Property . This should prevent users from using the ESC key while the PrintPreview window is on display. No message box required.

The issue now is that this doesn't allow you to select the number of copies or Printer name. But then, why would you need that if you are already giving the user the choice to select the required printing settings from the PrintPreview window.

VBA Code:
Option Explicit

#If VBA7 Then
    Declare PtrSafe Function RegisterHotKey Lib "user32" (ByVal hwnd As LongPtr, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    Declare PtrSafe Function UnregisterHotKey Lib "user32" (ByVal hwnd As LongPtr, ByVal id As Long) As Long
#Else
    Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
#End If
 
Sub PrintChq()
    Sheets("Sheet2").Select
    DisableEscapeKey = True
    ActiveSheet.PrintPreview
    DisableEscapeKey = False
End Sub

Private Property Let DisableEscapeKey(ByVal bDisable As Boolean)
    If bDisable Then
        Call RegisterHotKey(Application.hwnd, 1&, 0&, VBA.vbKeyEscape)
    Else
        Call UnregisterHotKey(Application.hwnd, 1&)
    End If
End Property
Let me try this...

This VBA is to print on preprinted stationary form which has restricted length of characters (amount in words) in a cell and there should not be any word wrapping. The preview is just to check there is no wrapping of the text in a cell. If there is wrapping user have to adjust the length of the text (for which there is a separate control, which works perfect)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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