locked
Modification of mailitem.body on Reply-Click has strange behaviour RRS feed

  • Question

  • Hi,

    i have following Problem:
    I want to write a small Addin for Outlook 2010 (with C#) which adds some content to the body of a reply-message.
    (Means: A user clicks on reply, the body of the message is modified, and then the user can write whatever he want in the reply-message)

    I think, the following example should add a "foo" in the subject-line, and the body of the reply-message should only contain "bar".

     

     

    void MailItem_Reply(Object response, ref bool cancel)
    {
    ((Outlook.MailItem)response).Subject = "foo";
    ((Outlook.MailItem)response).Body = "bar";
    }
    

     

     

     

    The behaviour is VERY unexpected:
     - The Subject of the reply-message is foo - thats ok.
     - But the Body of the reply-mail is NOT changed, instead, the Body of the ORIGINAL-Mail is changed to "bar" (on reselecting). Original content lost... omg?

    Has anyone an idea whats the problem, and how i can solve it?

    thanks!

    Sunday, November 7, 2010 11:38 PM

Answers

  • You would need to handle the Inspectors.NewInspector() event which would be fired when the reply Inspector opens. You could also try using the item.GetInspector() method to get an Inspector when you get a handle to the reply item.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "metal-heli" <=?utf-8?B?bWV0YWwtaGVsaQ==?=> wrote in message news:d8c94309-fb22-4179-8598-1f6959b9a6fa...

    Oh, i will do everything necessary in my code to fix this Problem.

    How can i add the necessary inspector which i should activate?


    Ken Slovak MVP - Outlook
    • Marked as answer by metal-heli Monday, November 8, 2010 8:49 PM
    Monday, November 8, 2010 8:20 PM

All replies

  • The problem is you are overwriting whatever's in the Body, which when the reply is opened consists of the original text plus maybe a signature. By overwriting that you remove everything that was there before.
     
    If you used code something like this you would solve the problem:
     
    ((Outlook.MailItem)response).Body = "bar" + ((Outlook.MailItem)response).Body;
    That would place "bar" before any existing text in the item.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "metal-heli" <=?utf-8?B?bWV0YWwtaGVsaQ==?=> wrote in message news:4bbddce2-d09c-4140-9e00-4b433ecb5000...

    Hi,

    i have following Problem:
    I want to write a small Addin for Outlook 2010 (with C#) which adds some content to the body of a reply-message.
    (Means: A user clicks on reply, the body of the message is modified, and then the user can write whatever he want in the reply-message)

    I think, the following example should add a "foo" in the subject-line, and the body of the reply-message should only contain "bar".

     

     

    void MailItem_Reply(Object response, ref bool cancel)
    {
    ((Outlook.MailItem)response).Subject = "foo";
    ((Outlook.MailItem)response).Body = "bar";
    }
    

     

     

     

    The behaviour is VERY unexpected:
     - The Subject of the reply-message is foo - thats ok.
     - But the Body of the reply-mail is NOT changed, instead, the Body of the ORIGINAL-Mail is changed to "bar" (on reselecting). Original content lost... omg?

    Has anyone an idea whats the problem, and how i can solve it?

    thanks!


    Ken Slovak MVP - Outlook
    Monday, November 8, 2010 2:27 PM
  • Thanks for your suggestion, but that does not solve the Problem.
    When using your code, you get following behaviour:
    The Original-Message is changed and has now a "bar" at the beginning.
    The Reply-Message which is displayed in a new-window is furthermore unmodified and contents the original-message and signature, but no "bar".

    Maybe i didn't explained the problem in a good way, i will try it again:
    The behaviour of
    ((Outlook.MailItem)response).Subject = "foo"
    ;
    is as follows: Subject of Reply-Message is "foo", the Subject of the Original-Message on which you reply does not change.

    The behaviour of
    ((Outlook.MailItem)response).Body = "bar"
    ;
    is as follows: Body of Reply-Message is the "normal" Body you get when clicking on reply.  The Body of the Original-Message is changed to "bar" (displayed on "relselection" of the mail).

    Thats what i am not understanding. I want to modify the body of the Reply-Message and The MSDN-Library says "response is the new item being sent in response to the original message." But the Body-Property modifys the body of the original-message?
    The problem is you are overwriting whatever's in the Body, which when the reply is opened consists of the original text plus maybe a signature. By overwriting that you remove everything that was there before.
     
    If you used code something like this you would solve the problem:
     
    ((Outlook.MailItem)response).Body = "bar" + ((Outlook.MailItem)response).Body;
    That would place "bar" before any existing text in the item.
    Monday, November 8, 2010 3:46 PM
  • When are you changing the body? You should do it in the first Inspector.Activate() event in the newly opened reply.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "metal-heli" <=?utf-8?B?bWV0YWwtaGVsaQ==?=> wrote in message news:d5ed9b93-f396-4ec8-964c-582ce5c6621b...
    Thanks for your suggestion, but that does not solve the Problem.
    When using your code, you get following behaviour:
    The Original-Message is changed and has now a "bar" at the beginning.
    The Reply-Message which is displayed in a new-window is furthermore unmodified and contents the original-message and signature, but no "bar".

    Maybe i didn't explained the problem in a good way, i will try it again:
    The behaviour of
    ((Outlook.MailItem)response).Subject = "foo"
    ;
    is as follows: Subject of Reply-Message is "foo", the Subject of the Original-Message on which you reply does not change.

    The behaviour of
    ((Outlook.MailItem)response).Body = "bar"
    ;
    is as follows: Body of Reply-Message is the "normal" Body you get when clicking on reply.  The Body of the Original-Message is changed to "bar" (displayed on "relselection" of the mail).

    Thats what i am not understanding. I want to modify the body of the Reply-Message and The MSDN-Library says "response is the new item being sent in response to the original message." But the Body-Property modifys the body of the original-message?
    The problem is you are overwriting whatever's in the Body, which when the reply is opened consists of the original text plus maybe a signature. By overwriting that you remove everything that was there before.
     
    If you used code something like this you would solve the problem:
     
    ((Outlook.MailItem)response).Body = "bar" + ((Outlook.MailItem)response).Body;
    That would place "bar" before any existing text in the item.

    Ken Slovak MVP - Outlook
    Monday, November 8, 2010 5:53 PM
  • Hm, i don't use an Inspector, im working with eventhandlers. Here is my complete code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    
    namespace Zitierer
    {
     public partial class ThisAddIn
     {
      Outlook.Explorer currentExplorer = null;
      Outlook.MailItem mailItem;
    
      private void ThisAddIn_Startup(object sender, System.EventArgs e)
      {
       currentExplorer = this.Application.ActiveExplorer();
       currentExplorer.SelectionChange +=
        new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);
      }
      private void CurrentExplorer_Event()
      {
       if (this.Application.ActiveExplorer().Selection.Count == 1
        && this.Application.ActiveExplorer().Selection[1] is Outlook.MailItem)
       {
        if (mailItem != null)
        {
         ((Outlook.ItemEvents_10_Event)mailItem).Reply -= new Outlook.ItemEvents_10_ReplyEventHandler(MailItem_Reply);
        }
    
        mailItem = this.Application.ActiveExplorer().Selection[1];
        ((Outlook.ItemEvents_10_Event)mailItem).Reply += new
         Outlook.ItemEvents_10_ReplyEventHandler(MailItem_Reply);
       }
      }
    
      private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
      {
      }
    
      void MailItem_Reply(Object response, ref bool cancel)
      {
    
       ((Outlook.MailItem)response).Subject = "foo";
       ((Outlook.MailItem)response).Body = "bar" + ((Outlook.MailItem)response).Body;
    
    
      }
    
    
      #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
     }
    }
    
    
    EDIT: using an Example-Code for Inspectors from the msdn-library: http://msdn.microsoft.com/en-us/library/cc668191.aspx which modifies new mail-messages. Same Problem: select a message, cick on Reply and the behaviour is the same as described in my previous post!
    Monday, November 8, 2010 7:31 PM
  • I found the same thing you're finding with wiping out the text or overwriting it, the Inspector.Activate() event is the first event where you can change things without doing that if I recall correctly.
     
    When Reply is clicked an Inspector will open. That is where you should do what I said. If you don't want to handle Inspector.Activate() I have no idea of how to help you.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "metal-heli" <=?utf-8?B?bWV0YWwtaGVsaQ==?=> wrote in message news:bd343065-9fa3-4dc3-8641-43d0d935563f...

    Hm, i don't use an Inspector, im working with eventhandlers. Here is my complete code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    
    namespace Zitierer
    {
     public partial class ThisAddIn
     {
     Outlook.Explorer currentExplorer = null;
     Outlook.MailItem mailItem;
    
     private void ThisAddIn_Startup(object sender, System.EventArgs e)
     {
      currentExplorer = this.Application.ActiveExplorer();
      currentExplorer.SelectionChange +=
      new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);
     }
     private void CurrentExplorer_Event()
     {
      if (this.Application.ActiveExplorer().Selection.Count == 1
      && this.Application.ActiveExplorer().Selection[1] is Outlook.MailItem)
      {
      if (mailItem != null)
      {
       ((Outlook.ItemEvents_10_Event)mailItem).Reply -= new Outlook.ItemEvents_10_ReplyEventHandler(MailItem_Reply);
      }
    
      mailItem = this.Application.ActiveExplorer().Selection[1];
      ((Outlook.ItemEvents_10_Event)mailItem).Reply += new
       Outlook.ItemEvents_10_ReplyEventHandler(MailItem_Reply);
      }
     }
    
     private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
     {
     }
    
     void MailItem_Reply(Object response, ref bool cancel)
     {
    
      ((Outlook.MailItem)response).Subject = "foo";
      ((Outlook.MailItem)response).Body = "bar" + ((Outlook.MailItem)response).Body;
    
    
     }
    
    
     #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
     }
    }
    
    
    EDIT: using an Example-Code for Inspectors from the msdn-library: http://msdn.microsoft.com/en-us/library/cc668191.aspx which modifies new mail-messages. Same Problem: select a message, cick on Reply and the behaviour is the same as described in my previous post!

    Ken Slovak MVP - Outlook
    Monday, November 8, 2010 7:48 PM
  • Oh, i will do everything necessary in my code to fix this Problem.

    How can i add the necessary inspector which i should activate?

    Monday, November 8, 2010 8:10 PM
  • You would need to handle the Inspectors.NewInspector() event which would be fired when the reply Inspector opens. You could also try using the item.GetInspector() method to get an Inspector when you get a handle to the reply item.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "metal-heli" <=?utf-8?B?bWV0YWwtaGVsaQ==?=> wrote in message news:d8c94309-fb22-4179-8598-1f6959b9a6fa...

    Oh, i will do everything necessary in my code to fix this Problem.

    How can i add the necessary inspector which i should activate?


    Ken Slovak MVP - Outlook
    • Marked as answer by metal-heli Monday, November 8, 2010 8:49 PM
    Monday, November 8, 2010 8:20 PM
  • Okay, i tried:

    ((Outlook.MailItem)response).GetInspector.Activate();
    
    before modifying body...and it works!!!! Omg, thanks so much. And i have learnd a lil bit more about Outlook ;-)
    One more question, only for improving my Knowledge: I got a warning, wenn using Activate() because there is also a non-method "[...]InspectorEvents_10_Event.Activate" which is in conflict with "[...]_Inspector.Activate()"-Method. A Methodgroup is used.
    (hope translation is correct, i have german messages)
    What does that mean?

    Monday, November 8, 2010 8:43 PM
  • You're welcome :)
     
    What you ran into is that until the first Inspector.Activate() event fires you only get what's called a weak object reference to the Inspector and its CurrentItem. You can think of that object reference as not fully filled in until the Activate() event fires, at that time things such as the reply body are all filled in and can be worked with.
     
    That occurs in C# code because both a method and event of an object have the same signature. In this case you can use a temporary cast to an _Inspector object that should get rid of the warning:
     
    Outlook.Inspector insp = ((Outlook.MailItem)response).GetInspector();
    ((Outlook._Inspector)insp).Activate();
    insp = null;

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "metal-heli" <=?utf-8?B?bWV0YWwtaGVsaQ==?=> wrote in message news:273dca7a-04a6-43b3-bcd4-1f0842c39dce...

    Okay, i tried:

    ((Outlook.MailItem)response).GetInspector.Activate();
    
    before modifying body...and it works!!!! Omg, thanks so much. And i have learnd a lil bit more about Outlook ;-)
    One more question, only for improving my Knowledge: I got a warning, wenn using Activate() because there is also a non-method "[...]InspectorEvents_10_Event.Activate" which is in conflict with "[...]_Inspector.Activate()"-Method. A Methodgroup is used.
    (hope translation is correct, i have german messages)
    What does that mean?


    Ken Slovak MVP - Outlook
    Monday, November 8, 2010 8:56 PM
  • Thanks, everything clear now!
    What you ran into is that until the first Inspector.Activate() event fires you only get what's called a weak object reference to the Inspector and its CurrentItem. You can think of that object reference as not fully filled in until the Activate() event fires, at that time things such as the reply body are all filled in and can be worked with.
     
    That occurs in C# code because both a method and event of an object have the same signature. In this case you can use a temporary cast to an _Inspector object that should get rid of the warning:
     
    Outlook.Inspector insp = ((Outlook.MailItem)response).GetInspector();
    ((Outlook._Inspector)insp).Activate();
    insp = null;
    Monday, November 8, 2010 10:47 PM