ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   [how to] VBA catching close/print in printpreview (https://www.excelbanter.com/excel-programming/274537-%5Bhow-%5D-vba-catching-close-print-printpreview.html)

Héctor Miguel

[how to] VBA catching close/print in printpreview
 
(sorry for the cross-posting)
<this could be useful

I found no answer [other than Leo Huser] searching for in posts to several news groups [since 1997]
on how to take PrintPreview in vba-control [an easy manner], so...
The ONLY way "to control" PrintPreview is... = not to use PrintPreview!!! <= [as method]
instead, it should be used [e.g.] = "UserResponse = Application.Dialogs(xlDialogPrintPreview). Show"
given that showing a BuiltInDialog it will return False if canceled by the user [or by code]
also, due to _BeforePrint event will be twice triggered [when "first-showing" PrintPreview]
there will be necessary other [public boolean] variables "as support".

Note: within _BeforePrint event, if Cancel is set to True, it will be "same-treated" = Canceled by user [or code]

=== in a normal code module ===
' to detect if the process is "by code" or "normal" _
and if the user [or code] cancels PrintPreview / Print '
Public ViewByCode As Boolean, _
PrintByCode As Boolean, _
PrintedByCode As Boolean

' Test_macro '
Sub My_Preview()
MsgBox "Starting PrintPreview [by code]..." ' <= this is optional '
ViewByCode = True
PrintedByCode = Application.Dialogs(xlDialogPrintPreview).Show
PrintByCode = False
If Not PrintedByCode Then
MsgBox "User [maybe code?] has CANCELED [print / preview]."
End If
End Sub

=== in ThisWorkbook code module ===
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If ViewByCode Then
ViewByCode = False
PrintByCode = True
ElseIf PrintByCode Then
MsgBox "Printing process [can be] controlled by code..."
Else
MsgBox "Printing process is [should it be?] ""normal""..."
End If
End Sub

__HTH__
Regards,
Héctor.
MS-MVP [Mexico]




All times are GMT +1. The time now is 03:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com