More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 23-Apr-2019 03:18

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: intermediate    Code area: Basic Outlook Printer Friendly Version
Title: Find parent message in Inbox
Description: The key to finding the parent message for a reply or forward is knowing that all messages in a conversation have the same ConversationTopic value, while the ConversationIndex is increased by 5 bytes with each exchange. This VBA sample takes a MailItem as its parameter and searches the Inbox for a matching parent item.
Date: 27-Oct-2007  09:04
Code level: intermediate
Code area: Basic Outlook
Posted by: Sue Mosher
This message is displayed as VB.NET
 Function FindParentMessage(msg As Outlook.MailItem) _
           As Outlook.MailItem
    Dim strFind As String
    Dim strIndex As String
    Dim fld As Outlook.MAPIFolder
    Dim itms As Outlook.Items
    Dim itm As Outlook.MailItem
    On Error Resume Next
    strIndex = Left(msg.ConversationIndex, _
                    Len(msg.ConversationIndex) - 10)
    Set fld = Application.Session.GetDefaultFolder(olFolderInbox)
    strFind = "[ConversationTopic] = " & _
              Chr(34) & msg.ConversationTopic & Chr(34)
    Set itms = fld.Items.Restrict(strFind)
    Debug.Print itms.count
    For Each itm In itms
        If itm.ConversationIndex = strIndex Then
            Debug.Print itm.To
            Set FindParentMessage = itm
            Exit For
        End If
    Set fld = Nothing
    Set itms = Nothing
    Set itm = Nothing
End Function
All 30comments
Page [ 1 2 3 Next >>  
  27-Oct-2007  09:06   
Here is a usage example, to find and display the parent message of the currently open message:

Sub TestFindParentItem()
    Dim msg As MailItem
    On Error Resume Next
    Set msg = FindParentMessage(Application.ActiveInspector.CurrentItem)
    If Not msg Is Nothing Then
    End If
    Set msg = Nothing
End Sub


1) This sample searches only the Inbox. If you wanted to search the entire mailbox or .pst file, you'd need to use the Application.AdvancedSearch method instead of Items.Restrict.

2) This expression

    Left(msg.ConversationIndex, _
         Len(msg.ConversationIndex) - 10)

returns the ConversationIndex of the parent message because it truncates by 10 charactes (5 bytes).
  15-Feb-2008  02:22   
itm.ConversationIndex from Inbox is empty and it is not matching with strIndex. So, the method is returning nothing for me.

  15-Feb-2008  10:54   
Could you show the code statements you're using to instantiate a MailItem and call FindParentMessage()?
  19-Feb-2008  00:51   
Sue Mosher,
Here is the code:
Private Sub objoutlook_ItemSend(ByVal oItem As Object, Cancel As Boolean)
Dim tempMsg As MailItem
If TypeName(oItem) = "MailItem" Then
Set tempMsg = FindParentMessage(oItem)
End If
End Sub

The FindParentMessage returns nothing even if the parent mailitem is available Inbox.

  19-Feb-2008  01:16   
You may need to add a breakpoint and step through the code in the debugger to determine what's going on, paying attention to the value of ConversationTopic and looking at what messages are filtered by Restrict. If there something unusual about those items?

BTW, the Debug.Print statement isn't necessary; I forgot to take it out after my testing.
  19-Feb-2008  02:03   
Sue Mosher,
   I am facing this problem only in MS Outlook 2000. Is there a way to make it work in 2000?

  20-Feb-2008  00:31   
Jai, I would expect ConversationIndex and ConversationTopic to work the same in all Outlook versions, but if you're finding that Outlook 2000 has a different behavior, I can't help with that. You'll need to compare the ConversationIndex of related messages to try to determine what the structure is for Outlook 2000.
  23-Apr-2008  13:07   
I tried the code, its working fine.
you need to change "olFolderInbox" with "olFolderSentMail" in the following line

Set fld = Application.Session.GetDefaultFolder(olFolderInbox)
  23-Apr-2008  13:10   
This code is working fine for the mails received from outlook but not working for mail sent by other than outlook. What modification do i need for thais?
  23-Apr-2008  13:17   
Sohail, you can examine the value of ConversationIndex to see if it's possible to draw any conclusions about linkage between items. Use Outlook Spy or MFCMAPI.exe.
Page [ 1 2 3 Next >>