Home > Back-end >  Slow opening of any email after set value for an object on Application_ItemLoad
Slow opening of any email after set value for an object on Application_ItemLoad

Time:05-04

Basically I am using below code to run macro after I manually open a specific email.
The code and macro works ,but I noticed there is a slowness while opening of any email on outlook.
After many tests, I find out this line is the cause of issue:

Set MyItem = Item

And this the full code:

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

Kindly How to fix this issue?

CodePudding user response:

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

You may consider using other event handlers in Outlook - for selecting in the Explorer window you may try to handle the 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 event which is fired when an inspector becomes the active window, either as a result of user action or through program code.

If there is a specific reason to use the ItemLoad event handler in the code you may continue using the late-binding technology and keep the item defined as object.

Also don't forget to release corresponding objects in the Unload event of the Item-level events.

  • Related