Search code examples
c#outlookvstooffice-interop

How to exclude original messages in Outlook plugin


I am writing an Outlook 2010/2013 plugin with C#. My abridged code looks like this:

using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
public partial class ThisAddIn
{
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {            
        Application.ItemSend += Application_ItemSend;
    }

    void Application_ItemSend(object Item, ref bool Cancel)
    {
        Outlook.MailItem mailItem = Item as Outlook.MailItem;
        // Scan for keywords before the user sends the email.
    }
}

When the user hits the Send button on an email, the Application_ItemSend event handler executes. At that point, I want to scan the message body for certain keywords. However, I want to exclude older/original messages in that scan. I only want to scan the message that the user has just typed... mailItem.Body and mailItem.HTMLBody contain not only the message that the user is sending, but they also include all the previous messages in the entire thread/conversation. How can I filter those out?


Solution

  • I've not tested this myself yet, but this is on my todo list for our Addin as well. I haven't been able to find much documentation on this, but the OOXML of the mail (as well as the HTML) - at least in 2013 - contain something like the following:

    OOXML:

    <w:bookmarkStart w:id="0" w:name="_MailOriginal"/>
    

    HTML:

    <span style='mso-bookmark:_MailOriginal'>
    

    This bookmark should allow you to find the point where you can cut off the message - as far as I know there is no other option in the API.

    See here on how to get the OOXML of the mail:

    Word.Document document = mailItem.GetInspector.WordEditor;
    string xml = document.WordOpenXML;