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

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?

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

Automatically Closing Outlook Windows via Script/Macro

I'm hoping someone on here can point me in the right direction with some expertise. I have a situation with a client's outlook where we need to close any open draft windows after a new message/draft is opened. Ideally after the 4th new window has opened.
Any "new message" window that opens after this we need the script to close 1st window that opened. Either killing the process, or something similar.
Recently have been looking into Outlook macros, but am unsure if they will help in this instance. (Maybe they are?). Being more familiar with Powershell, figured we could start there.
Looking to get help writing a powershell script, macro, etc to do this on the backend.
The Outlook object model provides all the required events, methods and properties for that. So, VBA macros is the right choice if you don't need to distribute the solution on multiple machines. Otherwise, you need to consider developing a COM add-in instead (for example, a VSTO based one should work for you). See Walkthrough: Create your first VSTO Add-in for Outlook for more information.
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.
You can also check the number of opened inspector windows in Outlook by using the Inspectors.Count property which returns a long indicating the count of objects in the specified collection.
Finally, the Inspector.Close method closes the Inspector and optionally saves changes to the displayed Outlook item. For example, a VBA sample which closes the active inspector instance:
Sub CloseItem()
Dim myinspector As Outlook.Inspector
Dim myItem As Outlook.MailItem
Set myinspector = Application.ActiveInspector
Set myItem = myinspector.CurrentItem
myItem.Close olSave
End Sub

How to get the WordEditor property of the currently selected email in an explorer reading pane?

I implement context menus for emails, this includes the explorer reading pane. To get the text under the mouse at the time of a right click I use the WordEditor. I do not think there is any other way of finding out where the mouse has clicked.
'_olItem comes from the current selection
olInspector = CType(_olItem.GetInspector, Outlook.Inspector)
wDoc = CType(olInspector.WordEditor, Word.Document)
'then go off and work with word
For the Explorer reading pane is the only way to get the WordEditor by first calling GetInspector?
One reason for asking is that I see that for Inline responses Outlook has the ActiveInlineResponseWordEditor property.
My addin also listens for new inspectors
Private Sub oInsps_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles oInsps.NewInspector
'do something here with this inspector
End Sub
If I cannot avoid calling GetInspector to get the word editor then is there a property of the inspector at the time the newinspector event fires to tell me that this inspector is actually from the reading pane and from me calling GetInspector?
Use Explorer.PreviewPane.WordEditor.

Outlook Application_Quit Event Doesn't Fire

I have an event in the ThisOutlookSession module:
Public Sub Application_Quit()
Dim fso As Object
Dim rootFolder As String
Dim oFolder As Object
Dim oFile As Object
Dim filename As String
Dim fullpath As String
...Procedure...
End Sub
When this triggers, it works correctly. However, the Quit event will not fire unless I have opened up the VBE editor during the session. For example, if I open Outlook and immediately close it, the _Quit event will not fire. If I then open Outlook again, open the editor, and then close Outlook, the _Quit event will fire as expected.
See here for a similar issue- though I've tried everything listed there without success. Changing the Private/Public status of the event, restarting the PC- these don't seem to have any effect. I've added a blank Public _Startup event, which has also had no effect.
What the specific procedure is in the _Quit event is irrelevant- I've tried just having a simple Msgbox in there, and the same behavior is observed.
I'm in Outlook 2013 on Windows 10 Enterprise, Enable All Macros in the Trust center.
Any ideas at all would be greatly appreciated.
If Outlook is doing a fast shutdown, Application.Quit won't fire.
The best you can do is Explorer.Close on the last visible Explorer object.

Modifying default behavior of outlook buttons

Is it possible to modify the behavior of existing buttons in outlook (or generally in ms office programs)? E.g. can I make the "send mail" button show a dialog before sending a mail?
I know that you can make add ins and put them into ribbon but can you add certain behavior to existing controls?
Use Microsoft.Office.Interop.Outlook und handle the Send event. You can find the documentation here.
You have use your application reference to get the active inspector and by the active inspector you retrieve the message class:
(CType(inspector.CurrentItem, Outlook.ItemEvents_10_Event)).Send += New Outlook.ItemEvents_10_SendEventHandler(Inspector_Send)
Private Sub Inspector_Send(ByRef Cancel As Boolean)
... your code...
End Sub

How to execute VBA after Outlook Application_Startup event handler and after all folders sync?

I'm trying to run a macro that moves emails received before today to a cabinet folder whenever Outlooks starts. The problem is that the Application_Startup event handler happens before Outlook is completely loaded and folders have synced. Consequently, all the emails that came in last night aren't moved to the cabinet when I open Outlook in the morning.
To fix this I created a custom class to instantiate an Outlook.syncObject which syncs all folders and provides an event handler when the syncing is complete. I create an object from this class within the Application_Startup event handler. However, this sync doesn't seem to actually retrieve any emails and also seems to complete before Outlook has even loaded.
It seems like being able to execute code after Outlook has done all of it's startup processes would be a common feature request. Thanks for any help.
This sample code simply shows me how many unread emails are in my inbox. If I close Outlook, send myself an email, then open Outlook, I need Outlook to load and a full sync to occur before generating the messagebox with the number of unread emails in my inbox.
Oulook Application_Startup event handler:
Dim mySyncInstance As New mySync
Private Sub Application_Startup()
mySyncInstance.Initialize_handler
End Sub
Custom mySync Class Code:
Dim WithEvents mySync As Outlook.syncObject
Sub Initialize_handler()
Set mySync = Application.Session.SyncObjects.item(1)
mySync.Start
End Sub
Private Sub mySync_SyncEnd()
MsgBox Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).items.Restrict("[UnRead] = True").Count & _
" Emails are unread in the main inbox."
End Sub
I'm trying to run a macro that moves emails received before today to a cabinet folder whenever Outlooks starts.
You may consider handling the NewMailEx event of the Application class which is fired when a new item is received in the Inbox. So, you can get the mail item and decide whether you need to move it in a subfolder or not. This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item.
Also you can create a rule and assign a VBA macro sub. The incoming instance of the mail item object will be passed as a parameter. For example:
public sub test(mail as MailItem)
' do whatever you need
end sub
As an alternative way you can handle the ItemAdd event of the Items class which is fired when one or more items are added to the specified collection. This event does not run when a large number of items are added to the folder at once (more than 16).
Why not use Items.ItemAdd event on the Inbox folder?