none
Insert text at the start of mailitem.Body in VSTO Outlook using C# RRS feed

  • Question

  • I am trying to insert some text whenever user composing a new mail OR Reply OR ReplyAll. I am able to capture all these 3 events and work on editor after event the occurrence.

    Now I want to insert some text at the beginning of the editor(the editor box where user type message), for which I tried:

    1) Insert method:

    mailItem.Body = mailItem.Body.Insert(0, "Some Text");

    This line of code is throwing NullReferenceException.

    2) Next, I tried to use Native approach :

    mailItem.Body = "Some Text " + mailItem.Body;

    This is appending text at the starting of mail body, but the formatting vanishes which is not acceptable at all.

    I think HTMLBody can help, but no idea how to use it. Can anybody please help?

    Thursday, March 5, 2020 4:19 AM

All replies

  • Hi Ankit,

    Thank you for posting here.

    Try the following code to see if it helpful to you.

     Outlook.MailItem Outlook_MailtItem = (Outlook.MailItem)Item;
    
        if (Outlook_MailtItem.BodyFormat == Outlook.OlBodyFormat.olFormatHTML)
        {
            
            string link = "new string";
            Outlook_MailtItem.HTMLBody = link + Outlook_MailtItem.Body;
        }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, March 5, 2020 9:21 AM
  • Hi Timon,

    Thanks for the reply.

    The above code appended the "new string" at the beginning of mail body, but the formatting vanishes. Like : 

    " new string" followed by mail received before(or mail replying to)

    eg : new string Organization: TEMPEV Microsoft 365 preparedness for.............

    Also, it vanished formatting from all messages. :(

    I'll explain more about what I am trying to achieve : 

    I am trying to create a plugin for Outlook 365 that whenever a user compose a new mail OR Reply OR Reply To All, automatically some greeting text appears at the starting of new mail message.

    I am able to capture all 3 events(New/Reply/Reply All), but whenever inserting text inside the new editor window either the text appends at last of new mail body or all the formatting vanishes.

    I am not so much familiar with VSTO, so took reference from Internet and wrote this piece of code : 

            Outlook.Inspectors inspectors;
            private Outlook.Application _application = null;
            Outlook.Explorer currentExplorer = null;
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                _application = Globals.ThisAddIn.Application;
                _application.ItemLoad += new Outlook.ApplicationEvents_11_ItemLoadEventHandler(_application_ItemLoad);
    
                inspectors = this.Application.Inspectors;
                inspectors.NewInspector +=
                new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
    
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
                // Note: Outlook no longer raises this event. If you have code that 
                //    must run when Outlook shuts down, see https://go.microsoft.com/fwlink/?LinkId=506785
            }
    
    
            Outlook.MailItem mi = null;
    
            private void GenerateItemMessage(object item, string operation)
            {
                mi = item as Outlook.MailItem;
                currentExplorer = this.Application.ActiveExplorer();
                currentExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);
            }
    
            private void CurrentExplorer_Event()
            {
                if (this.Application.ActiveExplorer().Selection.Count > 0)
                {
                    Outlook.MailItem selObject = this.Application.ActiveExplorer().Selection[1];
                    if (selObject.BodyFormat == Outlook.OlBodyFormat.olFormatHTML)
                    {
    
                        string link = "new string" + Environment.NewLine;
                        selObject.HTMLBody = link + selObject.Body;
                    }
                }
            }
            private void ThisAddIn_ReplyAll(object item, ref bool cancel)
            {
                GenerateItemMessage(item, "ReplyToAll");
            }
            private void ThisAddIn_Reply(object item, ref bool cancel)
            {
                GenerateItemMessage(item, "Reply");
            }
    
            Outlook.ItemEvents_10_Event _item;
            private void _application_ItemLoad(object Item)
            {
                _item = (Outlook.ItemEvents_10_Event)Item;
                _item.ReplyAll += new Outlook.ItemEvents_10_ReplyAllEventHandler(ThisAddIn_ReplyAll);
                _item.Reply += new Outlook.ItemEvents_10_ReplyEventHandler(ThisAddIn_Reply);
    
    
            }
    
            void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
            {
                if (greetings.greetingEnabled == true)
                {
                    Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
                    if (mailItem != null && mailItem.EntryID == null)
                    {
                        
                        mailItem.HTMLBody = mailItem.HTMLBody.Insert(0, "Hi");
                    }
                }
                
            }
    
    
            #region DODMethods
            string finalGreets(string recievers)
            {
                
                    if (recievers is null)
                        return greetings.greetingText + " ," + Environment.NewLine;
                    if (recievers.Split(Convert.ToChar(";")).Length > 1)
                        return greetings.greetingText + " all," + Environment.NewLine;
                    else
                        return greetings.greetingText + " " + greetings.greetingGender + " " + recievers + "," + Environment.NewLine;
    
                
                
                
                
            }
            #endregion
    
            #region VSTO generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion

    Method CurrentExplorer_Event() is responsible to add text inside new mail message body(Where I have wrote the above code provided by you).

    I observed one more issue into this, that whenever reply/reply all event occurs, the defined action is performing twice.

    Please help me in achieving this task.

    Best Regards,

    Ankit Singh Rawat


    • Edited by Ankit S Rawat Thursday, March 5, 2020 4:01 PM Observed one more issue.
    Thursday, March 5, 2020 2:37 PM
  • Hi Ankit,

    Since this question is related to VSTO, so I suggest that you can ask this question in Visual Studio Tools for Office (VSTO) forum, they can provide more professional opinions and suggestions.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 6, 2020 8:38 AM
  • Ok Timon, I'll do that.

    Thanks for your help.

    Friday, March 6, 2020 9:41 AM