none
Programmatically Insert HTML into outlook message RRS feed

  • Question

  • my code looks like this:

     

      private Outlook.Application Outlook_App
      Outlook_App = Globals.ThisAddIn.Application.Application;         
      Outlook_App.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Outlook_App_ItemSend);
    
    
    public void Outlook_App_ItemSend(object Item, ref bool Cancel)
    {
    
      Outlook.MailItem Outlook_MailtItem = (Outlook.MailItem)Item;
    
      Outlook.Inspector inspector = Outlook_MailtItem.GetInspector;
      Word.Document document = (Word.Document)inspector.WordEditor;
      Word.Range range = document.Range(0, 0);
      
      if (Outlook_MailtItem.BodyFormat == Outlook.OlBodyFormat.olFormatHTML)
        range.InsertAfter("<span> test </span>");
      
      Cancel = false;
    
    }
    

    It inserts the text, but the literal version. I see "<span> test </span>" in the email instead of "test".  How do you get it to take HTML as HTML?

     

     

     

    Monday, October 21, 2013 9:18 PM

Answers

  • Hi,

    I think you should set mailItem.HTMLBody when you want to add the html to the body.

    Since you want to add the html before to the content of the mail body, I suggest you to write the code as followed:

    public void Outlook_App_ItemSend(object Item, ref bool Cancel)
    {
        Outlook.MailItem Outlook_MailtItem = (Outlook.MailItem)Item;
    
        if (Outlook_MailtItem.BodyFormat == Outlook.OlBodyFormat.olFormatHTML)
        {
            //range.InsertAfter("<span> test </span>");
            string link = "<span> test </span>";
            Outlook_MailtItem.HTMLBody = link + Outlook_MailtItem.Body;
        }
    
        Cancel = false;
    }

    • Marked as answer by George Takei Tuesday, October 22, 2013 7:14 PM
    Tuesday, October 22, 2013 12:06 PM

All replies

  • Hi,

    I think you should set mailItem.HTMLBody when you want to add the html to the body.

    Since you want to add the html before to the content of the mail body, I suggest you to write the code as followed:

    public void Outlook_App_ItemSend(object Item, ref bool Cancel)
    {
        Outlook.MailItem Outlook_MailtItem = (Outlook.MailItem)Item;
    
        if (Outlook_MailtItem.BodyFormat == Outlook.OlBodyFormat.olFormatHTML)
        {
            //range.InsertAfter("<span> test </span>");
            string link = "<span> test </span>";
            Outlook_MailtItem.HTMLBody = link + Outlook_MailtItem.Body;
        }
    
        Cancel = false;
    }

    • Marked as answer by George Takei Tuesday, October 22, 2013 7:14 PM
    Tuesday, October 22, 2013 12:06 PM
  • Thank you. That took care of the HTML issue  (tho I did Outlook_MailtItem.HTMLBody = link + Outlook_MailtItem.HTMLBody; but that's a minor point)

    Apparently RTF also does html, I just discovered. Or hyperlinks at least. Assuming my <span> was a link, which I think you may have already done, do you happen to have any advice for how to handle RTF? It's a lot more crashy...

    Tuesday, October 22, 2013 3:47 PM