'How to capture the MessageID of an email that was just sent in Outlook using VBA
I generate an email in vba and send it. I have a class which includes the send event for that email so I am able to tell if the email has been sent. My issue is I need to capture the MessageID property once the email has been sent. I thought I could use the class variable and get the properties from it but it seems as though there is no message ID but that is not working so I am assuming the messageID does not come until a later event. So is there some other event I can hook onto which occurs after the messageID is generated? I thought maybe unload would work but it does not seem to. Also, I thought about trying to use the outmail object after the send but I guess outmail object becomes nothing after it is sent which is why I am trying to do this in the class.
My code in my class event is as follows:
Option Explicit
Public WithEvents oMailItem As Outlook.MailItem
Public WithEvents oOutlookApp As Outlook.Application
Public Sub oMailItem_Send(Cancel As Boolean)
'Find what version number from previous query of base review table --- ActiveVersion
If Err.Number <> 0 Then
Cancel = True
EmailSent = False
Else
EmailSent = True
MsgBox oMailItem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x1035001E")
End If
End Sub
Solution 1:[1]
You need to handle the ItemAdd event of the Items
class for the Sent Items
folder, it is fired when one or more items are added to the specified collection.
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
MsgBox Item.Subject
End Sub
Solution 2:[2]
When Outlook sends a message in the cached mode, it hands the message to the Exchange server and moves the message to the local Sent Items folder. Exchange Server then actually sends the messages and moves it to the server-side Sent Items folder. The message now has the message id, but Outlook assumes the messages are the same (to an end user) and does not sync the two (just an optimization trick).
If you access the cached message, message id won't be present - you need to open its online version, You can do that using using Extended MAPI (C++ or Delphi, use MAPI_NO_CACHE
bit when calling IMsgStore::OpenEntry
) or Redemption (I am its author - any language). Note that if you use Items.ItemAdd
event in the cached mode to open the online version of the message, you can get an error if the server is still sending/moving the message. You really need to use Items.ItemAdd
event on the online folder.
In case of Outlook Object Mode alone, the only thing you can do is turn the cached mode off and use the Items.ItemAdd
event. In case of Redemption, you can leave the cached mode on and use something similar to the script below:
Public WithEvents myItems As Redemption.RDOItems
MAPI_NO_CACHE = &H0200
PR_INTERNET_MESSAGE_ID = "http://schemas.microsoft.com/mapi/proptag/0x1035001F"
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set sentItemsFolder = Session.GetDefaultFolder(olFolderSentMail)
'reopen in the online mode
set items = Session.GetFolderFromID(sentItemsFolder.EntryID, , MAPI_NO_CACHE).Items
Private Sub myItems_ItemAdd(ByVal Item As Object)
MsgBox Item.Fields(PR_INTERNET_MESSAGE_ID)
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eugene Astafiev |
Solution 2 |