Braxvan
Braxvan

Reputation: 47

Store currently viewed Outlook email body into string

I am creating a VSTO add-in to capture the user's currently selected email (the one they are reading) and send that selected text as a string to a python script for processing.

I am not sure how to take the currently viewed email's body and store it into a single string. I have run across solutions using mailItem.Body to add text into a newly created email, I cannot find a way to get the text within the body from the email being viewed in the users inbox.

I was thinking something like this might work:

public void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
    Microsoft.Office.Interop.Outlook.MailItem mailItem = 
    Inspector.CurrentItem as Microsoft.Office.Interop.Outlook.MailItem;
    string test = mailItem.Body; //store email body as string
    MessageBox.Show(test); //verify the string was properly stored
 }

However, I believe the code above would only work if a user wanted the text from an email currently being written? What can I use to get the text from an email's body?

Upvotes: 2

Views: 1254

Answers (1)

Shyam sundar shah
Shyam sundar shah

Reputation: 2523

You can use following code for getting selected email body text

public partial class ThisAddIn
{
   private Outlook.Explorer currentExplorer = null;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        currentExplorer = this.Application.ActiveExplorer();
        currentExplorer.SelectionChange += ExplorerSelectionChange;
    }
    private void ExplorerSelectionChange()
    {
        if (this.Application.ActiveExplorer().Selection.Count > 0)
        {
            Object selItem = this.Application.ActiveExplorer().Selection[1];
            if (selItem is Outlook.MailItem)
            {
                Outlook.MailItem mailItem = (selItem as Outlook.MailItem);
                string bodyText= mailItem.Body; //GET PlainTExt
                 string bodyHTML=mailItem.HTMLBody; //Get HTMLFormat
             }
         }
     }
}

Upvotes: 3

Related Questions