none
outlook custom email render/view/ link RRS feed

  • Question

  • Hi

     I would like to write add in for outlook, to render text custom format.

    For example, if the outlook email  contains some key words, I will have to make/display highlighted orCLICKABLE without changing the word contents. Basically I wanted to change the email display without changing the content (override render engine). Since  I'm new to office development, can someone shade some light on this.

    When the user open the outlook email, I want highlight or create link on that pre-defind texts without changing the content. User should not be prompted for save when he closes the email. 

    should be able do this on readonly email/documents also.

    -Bala


    Balamurali C


    Balamurali C

    Monday, February 16, 2015 10:27 AM

Answers

All replies

  • Hello Bala,

    The Outlook object model model provides three main ways for working with item bodies:

    1. Body - a raw text.

    2. HTMLBody - a string representing the HTML body of the specified item.

    3. WordEditor - the Microsoft Word Document Object Model of the message being displayed. 

    So you can use the HTMLBody and WordEditor properties for customizing the message body. You can read more about these ways in the Chapter 17: Working with Item Bodies article in MSDN.

    If you need to replace the content you may also consider injecting your own form to replace the body view in Outlook inspectors. The CompleteReplacement layout is implemented by Add-in Express. See Advanced Outlook view and form regions . You can do the same on your own using Windows API. The Adjacent Windows In Outlook article describes the process in depth.

    Monday, February 16, 2015 11:46 AM
  • Hello Astafiev,

     Thanks for your quick reply., I just implemented same as described in the link. I'm getting exception when I'm am  trying to add link.

    "This method or property is not available because the document is locked for editing"

    If I make that mail editable (through Message->Actions->Edit Message) then its works well, but when I'm closing the inspector window Outlook prompting like 

    Body of the message "xxxx" has been changed 

    Do you want to save the changes to this message?

     

    -Bala


    Balamurali C

    Monday, February 16, 2015 12:40 PM
  • If I unprotect the document and try to add the link, I'm getting exception like command not supported.

    -Bala


    Balamurali C

    Monday, February 16, 2015 12:41 PM
  • Bala,

    > If I unprotect the document and try to add the link, I'm getting exception like command not supported.

    What code do you use? And where do you run it?

    > but when I'm closing the inspector window Outlook prompting like 

    You need to save changes calling the Save method or use an adjacent window like I described above. In that case you can display a replacement control for rendering a copy of  the message body.

    Monday, February 16, 2015 1:29 PM
  • Hi Astafiev 

     Thanks for your reply.

    >>What code do you use? And where do you run it?

    I'm using C#, and trying change the mailitem content when the MailItem open event. as

            Outlook.MailItem tmp = (Outlook.MailItem)((BTBCOLMailItemWrapper)(obj)).m_internalObj;
    Word.Document doc = (Word.Document)tmp.GetInspector.WordEditor ;
    //doc.Hyperlinks.Add( "www.google.com", "", "", "CLICK ON ME", ""); // exception
    doc.Content.InsertAfter("NEW");  //exception 

    when I execute these above 2 lines I'm getting exception as  "method or property is not available because the document is locked for editing"


    >>You need to save changes calling the Save method or use an adjacent window like I described above. In that case you can display a replacement control for rendering a copy of  the message body.

    I don't want save the changes, as I said I just want change the display (if I can override/change the render engine that would be really nice)

    Thanks 

    -Bala


    Balamurali C

    Tuesday, February 17, 2015 3:47 AM
  • BTBCOLMailItemWrapper is the wrapper class around MailItem object and m_internalObj is member of type MailItem. 

    Balamurali C

    Tuesday, February 17, 2015 3:50 AM
  • Can you please explain more on  

    " you can display a replacement control for rendering a copy of  the message body." 


    Balamurali C

    Tuesday, February 17, 2015 3:53 AM
  • Hi Astafiev

     I got your link, going through this (replacement control).


    Balamurali C

    Tuesday, February 17, 2015 4:08 AM
  • this "replacement window" they are taking about reading pane, window replacement. I'm looking for overriding mail message render (_WwG class)

    -Bala


    Balamurali C

    Tuesday, February 17, 2015 5:12 AM
  • To change the body view without touching the source mail item you can display your own form in inspectors where you can show a changed body content. The Outlook extensibility model doesn't provide anything for that (nor Outlook form regions). You are free to use Windows API functions to subclass Outlook windows or use any third-party components. Please refer to my first message above for more information.
    Tuesday, February 17, 2015 8:21 AM
  • HI Astafiev 

     I tried with following code., works as expected; but when I close OUTLOOK (Main Outlook window outlook.exe ) after few seconds its showing pop-up window for save changes,  exactly two times as pasted below :(


    I tried (many opens & closes) many mail items, but the pop-up showing only mail lastly which I opened and closed.

    Hope I have released all COM objects , please suggest me if I missing something here.

    --------------------------

    Microsoft Outlook
    ---------------------------
    The properties of the message "Test" have been changed.

    Do you want to save changes to this message?

    ---------------------------
    Yes   No   Cancel   
    ---------------------------

     public partial class ThisAddIn
     {
    ....
    ....

    private void Application_ItemLoad(object item)
    {
    if (item is Outlook.MailItem)
    {
    Outlook.MailItem tmpMailitem = (Outlook.MailItem)item;
    MyMailItem tmp1 = new MyMailItem(tmpMailitem);
    m_wrappedItems[new Guid()] = tmp1;
    }
    }
    ..
    ..
     }


    class MyMailItem 
    {
    private bool m_changed = false;
    public Outlook.MailItem m_mailItem { get; private set; }

    public MyMailItem(Outlook.MailItem mailItem)
    {
    m_mailItem = mailItem;
    RegisterEvents();
    }

    private void RegisterEvents()
    {
    ((Outlook.ItemEvents_10_Event)m_mailItem).Close += new Outlook.ItemEvents_10_CloseEventHandler(Item_Close);
    m_mailItem.Read += new Outlook.ItemEvents_10_ReadEventHandler(Item_Open);
    }

    private void UnRegisterEvents()
    {
    ((Outlook.ItemEvents_10_Event)m_mailItem).Close -= new Outlook.ItemEvents_10_CloseEventHandler(Item_Close);
    m_mailItem.Read -= new Outlook.ItemEvents_10_ReadEventHandler(Item_Open);
    }

    private void Item_Close(ref bool Cancel)
    {
    Cancel = true; // tried setting both false and true
    UnRegisterEvents();
    try
    {
    ((Outlook._MailItem)m_mailItem).Close(Outlook.OlInspectorClose.olDiscard);  //
    Marshal.ReleaseComObject(m_mailItem); // tried both In TIMER FUNCTION here too
    m_mailItem = null;
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    }
    catch (Exception e)
    { }
    }

    void Item_Open()
    {
    if (m_changed) return;
    m_changed = true;
    m_mailItem.HTMLBody += "       A";
    }
    }

    -Bala


    Balamurali C



    • Edited by techpach Thursday, February 26, 2015 11:47 AM
    Thursday, February 26, 2015 8:42 AM
  • Hello Bala,

    There is no need to use the ReleaseComObject method if the GC methods are called.

    Try to use the solution described in the Developing an Inspector Wrapper for Outlook 2010 article in MSDN. You can find the sample project in the Downloads section, see Outlook 2010: Developing an Inspector Wrapper .

    Do you get a message box with the sample code?

    Thursday, February 26, 2015 1:38 PM
  • Hi Astafiev

     Thanks for your quick reply.

     I tried that sample too.,  but when I change email body like 

    if (m_changed) return;
    m_changed = true;
    m_mailItem.HTMLBody += "       A";

    there too it started displaying message.

     I made this above sample to make the example simple. 


    Balamurali C

    Thursday, February 26, 2015 2:15 PM
  • https://www.add-in-express.com/creating-addins-blog/2008/10/30/releasing-office-objects-net/

    Here explained when to release.


    Balamurali C

    Thursday, February 26, 2015 2:16 PM
  • Hi Balamurali,

    For this issue, I suggest that you could open a new thread.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 27, 2015 2:19 AM
    Moderator