0

I want to change the body at reply (also at replyAll and forward), so that the composer already get the modified message. So the SendEvent is not a possible solution. But in the reply event the changes at HTMLBody affects the original message immediately and forever. Changes are also instant visible in another Outlook instance. The composer (inline and window) don't show the change on the first editing.

I checked the subject of the "response object" and it already has the prefix "RE:" and is really the response itself. I don't understand, how to change only the response part.

I could also use the inspector of the response, but I don't know how to make the changes with the HTML editor. Using the word editor sounds like an overkill, so I didn't try.

My very simplified code:

using System;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;

namespace OutlookAddInTEST
{
    public partial class ThisAddIn
    {
        private Outlook.Application _application = null;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            _application = Globals.ThisAddIn.Application;
            _application.ItemLoad += new Outlook.ApplicationEvents_11_ItemLoadEventHandler(_application_ItemLoad);
        }

        private void ThisAddIn_Reply(object response, ref bool cancel)
        {
            Outlook.MailItem mitem = (Outlook.MailItem)response;
            mitem.HTMLBody = mitem.HTMLBody.Replace("</body>", "# TEST # </body>");
        }

        Outlook.ItemEvents_10_Event _item;
        private void _application_ItemLoad(object Item)
        {
            _item = (Outlook.ItemEvents_10_Event)Item;
            _item.Reply += new Outlook.ItemEvents_10_ReplyEventHandler(ThisAddIn_Reply);
        }


    }
}
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Jonny
  • 335
  • 4
  • 15

1 Answers1

1

Instead of handling the ItemLoad event of the Application class you can handle the SelectionChange event of the Explorer class which is fired when the user selects a different or additional Microsoft Outlook item programmatically or by interacting with the user interface. So, you may subscribe to the events of currently selected item in the Explorer window. Don't forget to un-subsribe to the events and release the underlying COM objects when the item is not selected any longer.

Also you may handle NewInspector event which is fired whenever a new inspector window is opened, either as a result of user action or through program code. A better solution is to implement an item wrapper, see Implement a wrapper for inspectors and track item-level events in each inspector for more information.

Changes are also instant visible in another Outlook instance. The composer (inline and window) don't show the change on the first editing.

Use the ActiveInlineResponse property of the Explorer class which returns an item object representing the active inline response item in the explorer reading pane.

The Explorer.InlineResponse event is fired when the user performs an action that causes an inline response to appear in the Reading Pane. This event returns an item object representing the inline response item.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • If using inline response, use the `Explorer.InlineResponse` event (same name as the corresponding property). – Dmitry Streblechenko Mar 05 '23 at 22:22
  • Holy moly, what a fight against the `WordEditor`, but now with the help of an `ExplorerWrapper` and the `InlineResponse` event the repsonse body can be changed. But how to get the right event at a popup? The `NewInspector` event is a "nice" place where every reply fires, but I can not use the `WordEditor` property of this inspector (AccessViolation). I also tried the `Reply` event of the items, but the `WordEditor` is not in "EditMode", so a FindAndReplace will always fail (not allowed). – Jonny Mar 09 '23 at 08:10
  • 1
    Wait for the first Inspector's `Activate` event, `NewInspector` is too early for such tasks. – Eugene Astafiev Mar 09 '23 at 08:16
  • Nice. After implementing an `InspectorWrapper` and binding the `Activate` event and check the `EntryId` of the `CurrentItem`, I can freely change the body. Thanks guys :) – Jonny Mar 09 '23 at 09:09