单击“快速打印”按钮时,我试图根据工作表名称更改活动打印机,但是App_WorkbookBeforePrint
事件被触发两次。尝试过App_WorkbookBeforeClose
也触发了两次。我已经将“错误陷阱”更改为“完全出错”,但是似乎没有发生任何错误。
此工作簿:
Private XLApp As CExcelEvents
Private Sub Workbook_Open()
Set XLApp = New CExcelEvents
End Sub
类模块:
Option Explicit
Private WithEvents App As Application
Private Sub Class_Initialize()
Set App = Application
End Sub
Private Sub App_WorkbookBeforePrint(ByVal Wb As Workbook, Cancel As Boolean)
MsgBox Wb.FullName
assignPrinter
End Sub
模块:
Const printer1 As String = "Bullzip PDF Printer on Ne10:"
Const printer2 As String = "EPSONF7E8B5 (L565 Series) on Ne07:"
Public Sub assignPrinter()
Dim ws As Worksheet
Dim wsn As String
Set ws = ActiveWorkbook.ActiveSheet
wsn = ws.Name
Select Case wsn
Case "FGWIP"
Application.ActivePrinter = printer1
ws.PrintOut
Exit Sub
Case "Rework"
Application.ActivePrinter = printer2
ws.PrintOut
Exit Sub
Case Else
MsgBox "Else case."
Exit Sub
End Select
End Sub
更新:
使用App_SheetActivate
代替App_WorkbookBeforePrint
来更改活动打印机并删除ws.Printout
,如Matley所述
答案 0 :(得分:1)
我重新创建了您的模块,收到的唯一错误是代码 Debug.Print AssignPrinter 中,其中我替换为 assignPrinter 。
其余代码工作正常。 App_WorkbookBeforePrint 没有触发两次。您可以在 App_WorkbookBeforePrint 中设置一个断点,然后查看Stack以查看第二次触发 App_WorkbookBeforePrint 的触发器。