none
How to get the last email message from a conversation RRS feed

  • Question

  • I have a outlook addin in which I want to get the last email message from an email conversation that is currently open. I've tried searching but cannot find any relevant thread. I am new to .Net. Using Visual Studio 2010 C# with Outlook 2010.
    Wednesday, November 27, 2013 9:40 PM

Answers

  • Hello farazy,

    To get the job done you need to use the Conversation object from the Outlook Object Model. Below please find a sample code I prepared for you:

    void FindLastInConversation()
    {
        object selectedItem = OutlookApp.ActiveExplorer().Selection[1];
        // This example uses only 
        // MailItem. Other item types such as 
        // MeetingItem and PostItem can participate 
        // in the conversation. 
        if (selectedItem is Outlook.MailItem)
        {
            // Cast selectedItem to MailItem. 
            Outlook.MailItem mailItem = selectedItem as Outlook.MailItem;
            // Determine the store of the mail item. 
            Outlook.Folder folder = mailItem.Parent as Outlook.Folder;
            Outlook.Store store = folder.Store;
            if (store.IsConversationEnabled)
            {
                // Obtain a Conversation object. 
                Outlook.Conversation conv = mailItem.GetConversation();
                // Check for null Conversation. 
                if (conv != null)
                {
                    // Obtain Table that contains rows                      
                    Outlook.Table table = conv.GetTable();
                    int count = table.GetRowCount();
                    Debug.WriteLine("Conversation Items Count: " +  count.ToString());
                            
                    table.MoveToStart();
    
                    if(!table.EndOfTable)
                    {
                        // lastRow conatins the last item from the conversation
                        Outlook.Row lastRow = table.GetNextRow();                            
                        Debug.WriteLine(lastRow["Subject"]  + " Modified: " + lastRow["LastModificationTime"]);
                    }
               }
          }
    }

    Thursday, November 28, 2013 12:24 PM
  • Hello farazy,

    The Table class provides the Columns property which returns an instance of the Columns collection from the Outlook Object Model. To modify the default column set, use the Add, Remove, or RemoveAll methods of the Columns collection. The Add method can be used for adding the message body (see the Body and HTMLBody properties of the MailItem class).

    Friday, November 29, 2013 8:14 AM

All replies

  • Hello farazy,

    To get the job done you need to use the Conversation object from the Outlook Object Model. Below please find a sample code I prepared for you:

    void FindLastInConversation()
    {
        object selectedItem = OutlookApp.ActiveExplorer().Selection[1];
        // This example uses only 
        // MailItem. Other item types such as 
        // MeetingItem and PostItem can participate 
        // in the conversation. 
        if (selectedItem is Outlook.MailItem)
        {
            // Cast selectedItem to MailItem. 
            Outlook.MailItem mailItem = selectedItem as Outlook.MailItem;
            // Determine the store of the mail item. 
            Outlook.Folder folder = mailItem.Parent as Outlook.Folder;
            Outlook.Store store = folder.Store;
            if (store.IsConversationEnabled)
            {
                // Obtain a Conversation object. 
                Outlook.Conversation conv = mailItem.GetConversation();
                // Check for null Conversation. 
                if (conv != null)
                {
                    // Obtain Table that contains rows                      
                    Outlook.Table table = conv.GetTable();
                    int count = table.GetRowCount();
                    Debug.WriteLine("Conversation Items Count: " +  count.ToString());
                            
                    table.MoveToStart();
    
                    if(!table.EndOfTable)
                    {
                        // lastRow conatins the last item from the conversation
                        Outlook.Row lastRow = table.GetNextRow();                            
                        Debug.WriteLine(lastRow["Subject"]  + " Modified: " + lastRow["LastModificationTime"]);
                    }
               }
          }
    }

    Thursday, November 28, 2013 12:24 PM
  • Convesation object has following attributes.

    EntryID
    Subject
    CreationTime
    LastModificationTime
    MessageClass

    I need the message body. How can I get that?


    Thursday, November 28, 2013 9:50 PM
  • Hello farazy,

    The Table class provides the Columns property which returns an instance of the Columns collection from the Outlook Object Model. To modify the default column set, use the Add, Remove, or RemoveAll methods of the Columns collection. The Add method can be used for adding the message body (see the Body and HTMLBody properties of the MailItem class).

    Friday, November 29, 2013 8:14 AM