How to use Outlook's 'Add_Item' Event to Send an Email

Brian Myers 5 Reputation points
2023-08-23T19:15:54.63+00:00

I have a scenario where I need to have an Add_Item event for a specific (non-default) folder in the current Outlook session.

The scenario is that an email is created by code running in Excel.

I have this code working fine.

The email is saved, by code, in a specific folder in the current Outlook profile in an Exchange environment.

I need to add some code (presumably to the "ThisOutlookSession" module) that will detect when the 'EmailItem' is saved to the folder ('Add_Item' Event?), and automatically send the email.

I've done quite a bit of reading and testing and can't seem to even trigger the event, much less send the email(s).

The be fair though, if I could just trigger the event, I'm sure I could send the email.

I'm no stranger to Outlook VBA but this event is eluding me.

Here's the current incarnation of the code I'm trying to use...

Public Sub Initialize_handler()
Dim myOlItems As Outlook.items
     Set myOlItems = Application.GetNamespace("MAPI") _
                             .Stores("******@outlook.com") _
                             .GetRootFolder _
                             .folders("TestingAutoSend") _
                             .items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Outlook.MailItem)
     MsgBox "myOlItems_ItemAdd"
     Item.Send
End Sub

The code lives in the "ThisOutlookSession" module.

Macros are enabled.

The emails are minimally ready to send:

They contain a "To:" address

They contain a "Cc:" address

They contain a subject.

They have some message body.

I've read that I need to run the sub 'Initialize_handler' in order for the event to be detected and when I run it, it runs without error.

There are no errors when I drop a draft email in the folder.

The msgbox does not fire when I drop a draft email in the folder.

Please help me sort out this conundrum...

Thanks in advance!

Outlook
Outlook
A family of Microsoft email and calendar products.
4,504 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,340 questions
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.