none
outlook email sent item EntryID, how to obtain? RRS feed

  • Question

  • Hi,

    I'm trying to store the EntryID of an email so I can retrieve the email at a later date.

    However, it seems the EntryID changes when an email is moved, so when I obtain the EntryID of the email , the email resides in 'Drafts', however, when the email is sent the EntryID is no longer valid as the email is moved to 'Sent Items'.

    How do I get the EntryID of the sent email?

    I've tried to get the EntryID after I issue the .Send command using

    'send email
    oMsg.Send
         
    ' update emailID
     sEmailID = oMsg.EntryID 

    but that errors saying the 'Item has been moved or deleted'.

    So how do I track an email and keep it's unique ID from the drafts folder to the sent items so I can open it later?

    I've searched here and found some discussion on trapping the AddItem event on the sent items folder, but I can't find any code examples that relate to an outlook object in MS Access?

    I currently have this code for sending an email..

     
    ' Send email
     Public Sub Send()
         
        On Error GoTo Email_Error
         
        Dim oApp As Object
         Dim oMsg As Object
         Dim colAttach As Object
         Dim oAttach As Object
         Dim strEntryID As String
         Dim x As Integer
         Dim oReceipt As Object
         Dim oPA As Object
         Dim eml As Variant
         Dim att As Variant
         
        ' check outlook open
         Call checkOutlook
         
        ' create new Outlook MailItem
         Set oApp = CreateObject("Outlook.Application")
         Set oMsg = oApp.CreateItem(olMailItem)
            
        With oMsg
         
            'add recipients
             For Each eml In Recip
                 Set oReceipt = .Recipients.Add(eml)
                 oReceipt.Type = olTo
             Next
     
            'add BCC recipients
             For Each eml In BCC
                 Set oReceipt = .Recipients.Add(eml)
                 oReceipt.Type = olBCC
             Next
     
            'set email subject
             .Subject = Subject
             
            'set body  HTML
             .HTMLBody = Body
             
        End With
         
        'clear recipients
         Set oReceipt = Nothing
             
        'check items for mime encoding
         If Items.Count > 0 Then
             
            ' attach items
             Set colAttach = oMsg.Attachments
             For x = 1 To Items.Count
                 Set oAttach = colAttach.Add(Items.Item(x))
                 Set oPA = oAttach.PropertyAccessor
                 oPA.SetProperty PR_ATTACH_MIME_TAG, ItemTypes.Item(x)
                 oPA.SetProperty PR_ATTACH_CONTENT_ID, "item" & x
                 oPA.SetProperty PR_ATTACHMENT_HIDDEN, True
             Next
                        
        End If
             
        ' add normal file attachments
         For Each att In Files
             oMsg.Attachments.Add (att)
         Next
             
        ' save email
         oMsg.Save
            
        ' check if display or send
         If DispEmail Then
             oMsg.Display
         Else
             oMsg.Send
         End If
     
    
    Exit_Email:
     
        ' clean up email object
         Set oMsg = Nothing
         Set oAttach = Nothing
         Set oApp = Nothing
         Set oPA = Nothing
         
        Exit Sub
         
    Email_Error:
     
        ' can't open outlook
         If Err.Number = 287 Then
             sErrors = "Failed to open outlook, email process aborted"
             MsgBox sErrors
         Else
         ' standard error
             MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
             sErrors = Err.Description
         End If
         
        Resume Exit_Email
         
    End Sub

    Where in any of that do I stick what to capture the email being sent and obtain the sent items EntryID?

    All help greatly appreciated.

    Thanks,

    1DMF


    To err is human, to really balls things up you need Microsoft!




    • Edited by 1DMF Tuesday, December 18, 2012 11:56 AM
    • Moved by Dummy yoyoModerator Thursday, December 20, 2012 6:42 AM (From:Access for Developers)
    Tuesday, December 18, 2012 11:52 AM

Answers

  • Well this is an MS app that will always run from a user with Outlook, so I think I'll be OK.

    Note for anyone else trying to use the UserPorperties collection example above, as it's incomplete!

    There is an additional parameter that is not optional which provides data typing for the custom property.

    So in my case as the property I am trying to add is text i had to use 'olText' (or 1 is equivelant)

    As follows...

        Dim oApp As Object
        Dim oMsg As Object
        Dim sRef As String
        Const olText As Integer = 1
    
        Set oApp = CreateObject("Outlook.Application")
        Set oMsg = oApp.CreateItem(olMailItem)
        sRef = "My Email Reference"
    
        With oMsg
            .UserProperties.Add "EmailRef", olText
            .UserProperties.Item("EmailRef").Value = sRef
        End With
    
        oMsg.Save
    
    Hope it helps others.


    To err is human, to really balls things up you need Microsoft!

    Thursday, December 20, 2012 5:17 PM

All replies

  • You are correct that whomever designed the Outlook email/object identification system didn't understand basic database use of unchanging ID numbers. The result is a disaster for exactly what you're seeking.

    The only way around it we've found is to tag the email with a custom ID you create or append something to the subject line that you can reference and hope others don't modify.

    We've custom created solutions for some clients that work with the Exchange Server so all incoming and outgoing messages get logged into a SQL Server where we can have our own ID numbers independent of what Outlook does. It also simplifies how we search, track and display messages. After all, the Outlook inbox is usually people's largest database.


    Luke Chung
    Microsoft MVP
    President of FMS, Inc.
    Facebook Twitter

    Tuesday, December 18, 2012 10:45 PM
  • Hey Luke,

    Thanks for the reply. It seems totally bizzarre that there is no unique ID of an email from start to finish (Draft to Sent Items).

    I am now going down the route of unqiue identifier eiher in the body or the subject line (unless you know of a property that can be set on the email with a unique identifier no-one can change)

    I'm able to check the outbox and sent items and grab the email once it appears, but it isn't great and  I will have to factor in a lot of fault tollerance and 'timeout' settings so it doesn't infinately loop looking for an email that was never sent!

    Regards,

    Craig.


    To err is human, to really balls things up you need Microsoft!

    Wednesday, December 19, 2012 9:17 AM
  • Hi 1DMF,

    Welcome to the MSDN forum.

    I'm moving the thread for better support.

    Thanks for your understanding and have a nice day.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 20, 2012 6:42 AM
    Moderator
  • you can append unique tag in UserProperties of mailitem, something like:

    var userproperty = mailItem.UserProperties.Add("myCustomTag")

    userProperty.Value = "your value";

    mailitem.Save

    then alter in ItemAdd event of SentItems you can check if arrived mail has this property.

    As for waiting for mail to be sent, etc. - this is how it is, sending mail is an asynchronous process that can finish in few days or never, it has nothing to do with the way outlook API is designed. You have to also take into account that mail could be sent when your access is not running and you missed that, so when starting outlook from your code, you have to scan sentitems for all mails with your custom tag that you might have missed.

    Thursday, December 20, 2012 7:40 AM
  • Hi DamianD,

    Thanks for the reply, unfortunately listening for the ItemAdd event isn't possible as I use late bindings and you cannot apply the WithEvents operator to a variable defined as 'Object'.

    I am also not interested in any VBA that needs to be placed in Outlook, it needs to be a self contained Access application that merley uses Outlook.

    I certainly don't want to have to create some code that has to be put in all the user's Outlook that is triggered everytime they send an email, some machines are their own personal computers, you can't start putting crap like that on people's home computers, it's just wrong!

    However, I have made progress looking for the email in the sent items using the sent items collection and 'GetLast' and now you have provided a way of adding a unique ID that is permanent, I think I have all I need to code a workable solution.

    I really appreciate the input, many thanks.

    Craig.


    To err is human, to really balls things up you need Microsoft!

    Thursday, December 20, 2012 10:08 AM
  • Yea, every time the messsage changes folders, the ID gets changed. As you mentioned, adding an Outlook add-in to tag the message is not possible, and even if it were, it doesn't work if people access their emails from non-Outlook or non-PC devices. They only way we've been able to achieve it is at the Exchange level where there are events on the outgoing and incoming mail.

    Good luck!


    Luke Chung
    Microsoft MVP
    President of FMS, Inc.
    Facebook Twitter

    Thursday, December 20, 2012 4:14 PM
  • Well this is an MS app that will always run from a user with Outlook, so I think I'll be OK.

    Note for anyone else trying to use the UserPorperties collection example above, as it's incomplete!

    There is an additional parameter that is not optional which provides data typing for the custom property.

    So in my case as the property I am trying to add is text i had to use 'olText' (or 1 is equivelant)

    As follows...

        Dim oApp As Object
        Dim oMsg As Object
        Dim sRef As String
        Const olText As Integer = 1
    
        Set oApp = CreateObject("Outlook.Application")
        Set oMsg = oApp.CreateItem(olMailItem)
        sRef = "My Email Reference"
    
        With oMsg
            .UserProperties.Add "EmailRef", olText
            .UserProperties.Item("EmailRef").Value = sRef
        End With
    
        oMsg.Save
    
    Hope it helps others.


    To err is human, to really balls things up you need Microsoft!

    Thursday, December 20, 2012 5:17 PM