Results 1 to 4 of 4

Thread: get Outlook SentFolder last item

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    4

    get Outlook SentFolder last item

    I need to log certain information regarding a message I'm sending from within Access (using Redemption). So I tried to hook into the sentfolder.itemadd event, but I am not able to reliably capture that event (I'm sending the message and immediately try to get its entryid, but if the message has some large attachments it doesn't make it into the sent folder fast enough).

    Any suggestions?

    Thanks to all!
    VB Code:
    1. Option Compare Database
    2. Option Explicit
    3.  
    4. Dim WithEvents ObjOutlook As Outlook.Application, myNameSpace As Object
    5. Dim WithEvents objInspectors As Outlook.Inspectors
    6. Dim WithEvents objOpenInspector As Outlook.Inspector
    7. Dim WithEvents objMailItem As Outlook.MailItem
    8. Dim WithEvents colSentItems As Items
    9.  
    10. Dim sMailItem As Redemption.SafeMailItem, boSent As Boolean
    11. Dim sInspector As Redemption.SafeInspector
    12. Public Sub Class_Initialize()
    13.     boSent = False
    14.     Set ObjOutlook = CreateObject("Outlook.Application", "localhost")
    15.     Set objInspectors = ObjOutlook.Inspectors
    16. .........................................................................................
    17.  
    18. Private Sub colSentItems_ItemAdd(ByVal Item As Object)
    19. Dim strEntryID As String, sItem As Redemption.SafeMailItem
    20.  
    21. If Item.Class = olMail Then
    22.     Item.Save
    23.     Set sItem = CreateObject("Redemption.SafeMailItem")
    24.     sItem.Item = Item
    25.    
    26.     strEntryID = sItem.EntryID
    27.      vcLOG_SENT_EMAIL (strEntryID)
    28. End If
    Last edited by RobDog888; Aug 12th, 2005 at 10:54 AM. Reason: Added vbcode tags

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: get Outlook SentFolder last item

    Are you doing this from within Access? Why do you need it to be instant? Why not just read the items periodically?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    4

    Re: get Outlook SentFolder last item

    RobDog,
    I have no control over how many messages the user sends to the same recipient; so if I don't do it instantly how am I gonna reference the message to find it in the sent folder (and what if they empty the sent folder). I am only able to get the subject and I could also store the time the emailing was started,but that's about it (the EntryId gets re-written). Also this is a module to be plugged into an existing application that I haven't seen yet, so it's kinda tough to find an appropriate event to hook the periodic check.

    I have posted this same question to another forum and that's what Sue had to say:
    [I]Assuming you have properly instantiated colSentItems in your code, its ItemAdd event handler *does* wait until the item is transmitted and arrives in the SentItems folder. How long that will take depends entirely on your mail configuration.
    Maybe here is my problem? I call the class from a form:
    VB Code:
    1. 'send email
    2. Set mySafeEmail = New clsSendEmail
    3. Call mySafeEmail.EMAIL_SAFE(strEmailRecipient, strMessageType)
    4.  
    5. And the code for the class is:
    6.  
    7. Option Compare Database
    8. Option Explicit
    9.  
    10. Dim WithEvents ObjOutlook As Outlook.Application, myNameSpace As Object
    11. Dim WithEvents objInspectors As Outlook.Inspectors
    12. Dim WithEvents objOpenInspector As Outlook.Inspector
    13. Dim WithEvents objMailItem As Outlook.MailItem
    14. Dim WithEvents colSentItems As Items
    15. Dim strAttachmentFullName As String
    16.  
    17. Dim sMailItem As Redemption.SafeMailItem
    18. Public Sub Class_Initialize()
    19.     Set ObjOutlook = CreateObject("Outlook.Application", "localhost")
    20.     Set objInspectors = ObjOutlook.Inspectors
    21.     Set myNameSpace = ObjOutlook.GetNamespace("MAPI")
    22.     Set colSentItems = myNameSpace.GetDefaultFolder(olFolderSentMail).Items
    23. End Sub
    24. Private Sub Class_Terminate()
    25.     Set objOpenInspector = Nothing
    26.     Set objInspectors = Nothing
    27.     Set objMailItem = Nothing
    28.     Set colSentItems = Nothing
    29. End Sub
    30. Public Sub EMAIL_SAFE(strEmailAddress As String, strMessageType As String)
    31.  
    32. On Error GoTo EMAIL_SAFE_ERROR
    33.  
    34. 'we don't use redemption objects because we only add the recipient
    35. 'and open the new mailtiem
    36.  
    37. Dim objExplorer As Outlook.Explorer
    38. Dim objOutbox As Outlook.MAPIFolder
    39. 'show Outlook if not open
    40. If ObjOutlook.Explorers.Count = 0 Then
    41.     Set objOutbox = myNameSpace.GetDefaultFolder(olFolderOutbox)
    42.     objOutbox.Display
    43. End If
    44. 'new Outlook message
    45. Set objMailItem = ObjOutlook.CreateItem(olMailItem) 'Create a new message
    46.  
    47. If strMessageType = "HTML" Then
    48.     objMailItem.BodyFormat = olFormatHTML
    49. Else
    50.     objMailItem.BodyFormat = olFormatPlain
    51. End If
    52.  
    53. objMailItem.To = strEmailAddress
    54. 'show the message
    55. objMailItem.Display
    56.  
    57. EMAIL_SAFE_EXIT:
    58.     Exit Sub
    59. EMAIL_SAFE_ERROR:
    60.     MsgBox Err.Number & Err.Description
    61.  
    62. End Sub
    63. Private Sub vcLOG_SENT_EMAIL(strEntryID As String)
    64. 'this sub logs the email details into the database table
    65. 'it looks for the last sent item and checks to see if the recipient and time matches
    66.  
    67. Dim rstLog As DAO.Recordset
    68.  
    69. Dim oMailItem As MailItem
    70. Dim strCC As Variant, strbCC As String, strSender As String
    71. Dim strTimeSent As String, strBody As String, strRecipient As String
    72. Dim strSubject As String, strAttachment As String, iCount As Integer
    73. Dim oSentItemsAdd As Object
    74.  
    75. On Error GoTo vcLOG_EMAIL_ERROR
    76.  
    77. Set oSentItemsAdd = myNameSpace.GetDefaultFolder(olFolderSentMail)
    78. Set oMailItem = myNameSpace.GetItemFromID(strEntryID)
    79. Set sMailItem = CreateObject("Redemption.SafeMailItem")
    80. 'use Redeption object as we'll need to access protected properties
    81. sMailItem.Item = oMailItem
    82.    
    83. 'read the required data
    84.         strCC = sMailItem.CC
    85.         strbCC = sMailItem.bCC
    86.         strRecipient = sMailItem.To
    87.         strSender = sMailItem.SenderEmailAddress
    88.         strSubject = sMailItem.SUBJECT
    89.         strBody = sMailItem.BODY
    90.         strTimeSent = sMailItem.SentOn
    91.         strAttachment = ""
    92.         For iCount = 1 To sMailItem.Attachments.Count
    93.         'only  not embedded attachments
    94.             Dim strCID As String
    95.             strCID = sMailItem.Attachments.Item(iCount).Fields(&H3712001E)
    96.             If strCID = "" Then
    97.                strAttachment = strAttachment & ";" & sMailItem.Attachments.Item(iCount).Filename
    98.             End If
    99.         Next
    100.         If Len(strAttachment) > 1 Then strAttachment = Right(strAttachment, Len(strAttachment) - 1)
    101.  
    102. 'we need to add a record to the log table
    103. Set rstLog = CurrentDb.OpenRecordset("tblEmailLog", dbOpenDynaset)
    104.  
    105. rstLog.AddNew
    106.     rstLog("DATE_SENT") = Left(strTimeSent, InStr(strTimeSent, " "))
    107.     rstLog("TIME_SENT") = Right(strTimeSent, InStr(strTimeSent, " ") + 1)
    108.     rstLog("SEND_BY") = strSender
    109.     rstLog("SEND_TO") = strRecipient
    110.     rstLog("CC") = strCC
    111.     rstLog("bCC") = strbCC
    112.     rstLog("MESSAGE_ID") = strEntryID
    113.     rstLog("SUBJECT") = strSubject
    114.     rstLog("ATTACHMENT") = strAttachment
    115.     If Form_frmSendEmail.chkSave = True Then rstLog("BODY") = strBody
    116. rstLog.Update
    117.  
    118. rstLog.Close
    119. Set rstLog = Nothing
    120.  
    121.  
    122. vcLOG_EMAIL_EXIT:
    123. Exit Sub
    124. vcLOG_EMAIL_ERROR:
    125. MsgBox Err.Number & " " & Err.Description
    126. End Sub
    127. Private Sub colSentItems_ItemAdd(ByVal Item As Object)
    128. Dim strEntryID As String, sItem As Redemption.SafeMailItem
    129.  
    130. If Item.Class = olMail Then
    131.     Item.Save
    132.     strEntryID = Item.EntryID
    133.     vcLOG_SENT_EMAIL (strEntryID)
    134. End If
    135.    
    136. End Sub
    137. Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
    138.  
    139.     If Inspector.CurrentItem.Class = olMail Then
    140.  
    141.         Set objMailItem = Inspector.CurrentItem
    142.  
    143.         Set objOpenInspector = Inspector
    144.  
    145.     End If
    146.  
    147. End Sub
    148.  
    149. Private Sub objMailItem_Send(Cancel As Boolean)
    150. Dim sMAPI_Utils As MAPIUtils, i As Integer
    151.  
    152. Set sMAPI_Utils = CreateObject("Redemption.MAPIUtils")
    153.  
    154. sMAPI_Utils.DeliverNow
    155.    
    156. End Sub
    157.  
    158. Private Sub objOpenInspector_Close()
    159. 'lets check to see if the outbox has anything in it and wait if it does
    160. Dim l As Long, colOutbox As Items
    161.  
    162.  
    163. CHECK_OUTBOX:
    164.  
    165. Set colOutbox = myNameSpace.GetDefaultFolder(olFolderOutbox).Items
    166. If colOutbox.Count > 0 Then
    167.     'open the custom dialog form
    168.     If Not IsLoaded("frmSTILL_SENDING") Then
    169.         DoCmd.OpenForm "frmSTILL_SENDING"
    170.     End If
    171.    
    172.     For l = 1 To 5    ' Start loop.
    173.         DoEvents    ' Yield to operating system.
    174.         Sleep (100)
    175.     Next l
    176.    
    177.     If Form_frmSTILL_SENDING.lblCancel.Caption = "Cancel" Then
    178.     'means the user clicked the cancel button
    179.         MsgBox "THIS EMAIL HAS NOT BEEN LOGGED!", vbExclamation, "CANCELING"
    180.         If IsLoaded("frmSTILL_SENDING") Then DoCmd.Close acForm, "frmSTILL_SENDING"
    181.         Exit Sub
    182.     End If
    183.     'reneter the loop until the outbox is empty
    184.     GoTo CHECK_OUTBOX
    185. Else
    186.     'add a small loop to allow item to be added to the sent folder
    187.     For l = 1 To 5
    188.         DoEvents
    189.     Next l
    190.     If IsLoaded("frmSTILL_SENDING") Then DoCmd.Close acForm, "frmSTILL_SENDING"
    191. End If
    192.  
    193.  
    194. End Sub



    Do you see any obvious mistake?

    Thank you for your time!
    Gicu
    Last edited by RobDog888; Aug 12th, 2005 at 11:58 AM. Reason: Added vbcode tags for easier reading and removed competitor link ;)

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: get Outlook SentFolder last item

    About the only way you can truely track messages is to do this at the Exchange Server level (assuming that your in an Exchange environment ). Otherwise you wil always experience some kind of lag or loss of accurate logging. You can set up a secondary email account in Exchange to receive all emails in and out going very easily and accurately.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width