在 Application_ItemLoad 上为对象设置值后打开任何电子邮件都缓慢

Slow opening of any email after set value for an object on Application_ItemLoad

基本上,我在手动打开特定电子邮件后使用以下代码来 运行 宏。
代码和宏有效,但我注意到在 outlook 上打开任何电子邮件时速度很慢。
经过多次测试,我发现这一行是问题的原因:

Set MyItem = Item

这是完整代码:

Option Explicit
Option Compare Text
Public WithEvents MyItem As Outlook.MailItem

 
Private Sub Application_ItemLoad(ByVal Item As Object)
    If Item.Class = olMail Then
       Set MyItem = Item  'This line cause slow opening of any email
    End If
End Sub
 
Private Sub myItem_Open(Cancel As Boolean)
      If MyItem.Subject = "Test Email" Then
      'Code
      End If
End Sub

请问如何解决这个问题?

Basically I am using below code to run macro after I manually open a specific email.

您可以考虑在 Outlook 中使用其他事件处理程序 - 为了在 Explorer window 中进行选择,您可以尝试处理 SelectionChange event of the Explorer class which is fired when the user selects a different or additional Microsoft Outlook item programmatically or by interacting with the user interface. For opening in the separate window you can handle the NewInspector event which is fired whenever a new inspector window is opened, either as a result of user action or through program code. The event occurs after the new Inspector object is created but before the inspector window appears. Also you may handle the Inspector.Activate 事件,该事件在检查器变为活动状态时触发window,作为用户操作的结果或通过程序代码。

如果有特定原因需要在代码中使用 ItemLoad 事件处理程序,您可以继续使用 late-binding 技术并将项目定义为对象。

另外不要忘记在Item级事件的Unload事件中释放相应的对象。