Open native (not Sheet.PrintPreview) print preview window for non-active sheet?

vanowm

New Member
Joined
Jun 22, 2019
Messages
11
Hello.

I'm trying open "native" print preview window (the same window that opens when clicked print button in toolbar) for a specific sheet.
The only command I know that can do this is
Code:
Application.CommandBars.ExecuteMso "PrintPreviewAndPrint"
But I'm having problem open it for a specific sheet and stay on original sheet when the window is closed:

Code:
cur = ActiveSheet.Name
Sheets("Print").Select
'ActiveSheet.PrintPreview 'shows correct sheet
Application.CommandBars.ExecuteMso "PrintPreviewAndPrint" 'shows incorrect sheet
Sheets(cur).Select

The problem with this code is that print preview loads active sheet AFTER the rest of the code was executed.
So maybe it needs some kind of a "preview window closed" event listener and then switch to "original" sheet?

Any suggestions?

Thank you.
 
Last edited by a moderator:
Ok - I see what you mean .

Try this : in the (ThisWorkbook Module)
Code:
Option Explicit

[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
    Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If

Private WithEvents cmndbars As CommandBars
Private cur As String


Sub Test()

    On Error GoTo errHandler

    cur = ActiveSheet.Name

    [COLOR=#0000ff][B]EnableScreenUpdatingAPI = False[/B][/COLOR]
    
    Sheets("Print").Select
       
    Application.CommandBars.ExecuteMso "PrintPreviewAndPrint"
    
    Call Start_PrintPreviewAndPrint_CloseEventListener    


        [COLOR=#008000]'rest of your code here...[/COLOR]    


    Exit Sub
    
errHandler:
    [COLOR=#0000ff][B]EnableScreenUpdatingAPI = True[/B][/COLOR]

End Sub



Private Sub Start_PrintPreviewAndPrint_CloseEventListener()

    Set cmndbars = Application.CommandBars
    Call cmndbars_OnUpdate
    
End Sub


Private Sub cmndbars_OnUpdate()

    If FindWindowEx(Application.hwnd, 0, "FullpageUIHost", vbNullString) = 0 Then
        Sheets(cur).Select
        [B][COLOR=#0000ff]EnableScreenUpdatingAPI = True[/COLOR][/B]
        Set cmndbars = Nothing
        Debug.Print "done"
    End If
    
End Sub


Private Property Let EnableScreenUpdatingAPI(ByVal Enable As Boolean)

    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
        Dim hwnd As LongPtr
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
        Dim hwnd As Long
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If

    Const WM_SETREDRAW = &HB
    
    hwnd = FindWindowEx(FindWindowEx(Application.hwnd, 0, "XLDESK", vbNullString), 0, "EXCEL7", vbNullString)
    SendMessage hwnd, ByVal WM_SETREDRAW, ByVal CLng(Enable), 0&
    
End Property
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Now after exiting print preview the sheet is "frozen"
Doesn't freeze for me. I use excel 2016


Try this variation :
Code:
Option Explicit

[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
    Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
    Private Declare PtrSafe Function InvalidateRect Lib "user32" (ByVal hwnd As LongPtr, ByVal lpRect As Long, ByVal bErase As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function InvalidateRect Lib "user32" (ByVal hwnd As Long, ByVal lpRect As Long, ByVal bErase As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If

Private WithEvents cmndbars As CommandBars
Private cur As String


Sub Test2()

    On Error GoTo errHandler

    cur = ActiveSheet.Name

    [B][COLOR=#0000ff]EnableScreenUpdatingAPI = False[/COLOR][/B]
    
    Sheets("Print").Select
       
    Application.CommandBars.ExecuteMso "PrintPreviewAndPrint"
    
    Call Start_PrintPreviewAndPrint_CloseEventListener    


        [COLOR=#008000]'rest of your code here...[/COLOR]
    


    Exit Sub
    
errHandler:
    [B][COLOR=#0000ff]EnableScreenUpdatingAPI = True[/COLOR][/B]

End Sub


Private Sub Start_PrintPreviewAndPrint_CloseEventListener()

    Set cmndbars = Application.CommandBars
    Call cmndbars_OnUpdate
    
End Sub


Private Sub cmndbars_OnUpdate()

    If FindWindowEx(Application.hwnd, 0, "FullpageUIHost", vbNullString) = 0 Then
        Sheets(cur).Select
        Set cmndbars = Nothing
        [B][COLOR=#0000ff]EnableScreenUpdatingAPI = True[/COLOR][/B]
        Debug.Print "done"
    End If
    
End Sub


Private Property Let EnableScreenUpdatingAPI(ByVal Enable As Boolean)

    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
        Dim hwnd As LongPtr
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
        Dim hwnd As Long
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If

    Const WM_SETREDRAW = &HB
    
    hwnd = FindWindowEx(FindWindowEx(Application.hwnd, 0, "XLDESK", vbNullString), 0, "EXCEL7", vbNullString)
    SendMessage hwnd, ByVal WM_SETREDRAW, ByVal CLng(Enable), 0&
    [COLOR=#0000ff][B]InvalidateRect hwnd, 0, 0[/B][/COLOR]
    
End Property
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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