I have some code that runs through and collects some data to print off several sheets. each sheet is printed separately and loops through.
I use some code
Applications.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
to open the print preview page then some code in a form to either print with a click of a button or exit. This works ok but when it's finished looping through I am left with the print preview page still open.
Can you help me with the code to close the print preview page and return to the home tab.
Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
'opens the form "ContinuePrinting"
ContinuePrinting.Show
Do
DoEvents
Loop Until ContinuePrinting.Visible
'Wait until the form is closed
Do While ContinuePrinting.Visible
DoEvents
Loop
Application.CommandBars.ExecuteMso ("FilePrintQuick")
Sheets("SeqTaskPrint").Activate
Range("as2").Select
Next WP
Application.CommandBars.??????????????????
CodePudding user response:
You can use SendKeys ("{ESC}")
.
Note: SendKeys
are unreliable but can be useful if used properly.
Here is an example. The code will wait for 5 seconds before closing Print Preview
Code
Option Explicit
Sub ShowAndClosePrintPreview()
Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
'~~> Wait for 5 seconds
Wait 5
'~~> Close print preview
SendKeys ("{ESC}")
End Sub
Private Sub Wait(ByVal nSec As Long)
nSec = nSec Timer
While nSec > Timer
DoEvents
Wend
End Sub
In Action
CodePudding user response:
With a little research, I found the below Code Snippet which should work.
ActiveSheet.PrintOut preview:=False
Or ActiveSheet.PrintOut preview:=False
Source: Chandoo.Org