View Single Post
 
Old 08-23-2023, 11:59 AM
BilboBaggins BilboBaggins is offline Windows 10 Office 2013
Novice
 
Join Date: Aug 2023
Posts: 1
BilboBaggins is on a distinguished road
Default How to use Outlook's 'Add_Item' Event to Send an Email

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...

Code:
Public Sub Initialize_handler()
Dim myOlItems As Outlook.items

     Set myOlItems = Application.GetNamespace("MAPI") _
                             .Stores("redacted_alias@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!
Reply With Quote