[how to] VBA catching close/print in printpreview

H

Héctor Miguel

(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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top