我将一个VBA宏指定给一个按钮,该按钮隐藏工作表中的某些行和列,然后更改为该工作表并打开打印对话框。
我希望能够在打印对话框退出后再次取消隐藏所有这些行和列,方法是打印或取消。
我已经通过更改为工作表,打开打印对话框,然后显示消息框进行了简要测试。但是,消息框只显示在打印对话框的顶部。
newPlan.Sheets("Single_Plan").Activate
Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
MsgBox ("Print Complete")
如何在对话框关闭后让代码继续运行?
答案 0 :(得分:0)
不确定如何从该对话框中执行此操作。这虽然有效:
Application.Dialogs(xlDialogPrintPreview).Show
MsgBox "Now cleanup."
答案 1 :(得分:0)
Jaafar Tribak刚刚发布了一个适用于我的Excel 2019解决方案:
Option Explicit
#If 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
#Else
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
#End If
Sub Test()
Application.CommandBars.ExecuteMso "PrintPreviewAndPrint"
Do
DoEvents
Loop Until FindWindowEx(Application.hwnd, 0, "FullpageUIHost", vbNullString) = 0
'code after print preview window is closed (printed or canceled)
MsgBox ("Print Complete")
End Sub