none
outlook 2010 Reply Event c# RRS feed

  • Question

  • How does one capture the reply, reply all and forward in an add-in?

    I have tried using

    http://social.msdn.microsoft.com/Forums/en-US/outlookdev/thread/787c1d09-910c-44ea-aee8-a63c2955e71f

    but it is not working, meaning it never runs


    • Edited by niyack Friday, December 7, 2012 5:57 PM
    Friday, December 7, 2012 5:57 PM

Answers

  • There is a problem with Outlook where handlers tend to get garbage collected if they are not added to objects that won't go out of scope (declare them at the class level) so you will need to declare the "inspectors" variable at the class level before using this code:

    class level definition: Outlook.Inspectors inspectors;

    Code for handler:

    inspectors = this.Application.Inspectors;
    inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);

    Inspectors_NewInspector is obviously the method that will be called by the handler, but you could do something like:

    void Inspectors_NewInspector(Outlook.Inspector Inspector)
    {
    if (Inspector.CurrentItem is Outlook.MailItem)
    {
    Outlook.MailItem mailItem = Inspector.CurrentItem;
    // Do whatever you want with the mailItem, check if it's a reply/reply all/forward, etc.
    }
    }


    Friday, December 7, 2012 6:24 PM
  • Checking the subject is about the only way. You can tell from ConversationIndex that a new message is part of a conversation and not the first member of the conversation.
     
    If you can find the preceding message in the thread by using ConversationIndex and ConversationTopic you could use Extended MAPI or Redemption to read the MAPI PR_LAST_VERB_EXECUTED property and see if the last verb was EXCHIVERB_REPLYTOALL, or EXCHIVERB_REPLYTOSENDER, or EXCHIVERB_FORWARD.
     
    See http://www.outlookcode.com/codedetail.aspx?id=1714 for an example of finding the parent message.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Brian Atkinson" <=?utf-8?B?QnJpYW4gQXRraW5zb24=?=> wrote in message news:e080106b-9762-4ca3-8b14-080f91a314f7...
    Not sure if there is a property on a MailItem to tell you if a given MailItem is a forward or reply or not, but you could do something like check the MailItem's subject.  If it starts with RE: it's reply, FW: for forward, etc..  There may be a more reliable solution I haven't done much research on it, but it seems like that would work for now.  Your original method just doesn't seem viable, you'd literally have to attach the .Reply or ..Forward handler to every MailItem in the user's Inbox or Sent Mail folder for it to work.

    Ken Slovak MVP - Outlook
    Monday, December 10, 2012 2:14 PM
    Moderator

All replies

  • Are you trying to set handlers only on individual MailItem's reply/reply all/forward, or any time any MailItem is replied to, etc?  You could set a handlerfor a new Inspector window and determine if the inspector's MailItem is a reply/reply all/forward if you're looking for a global solution.
    Friday, December 7, 2012 6:07 PM
  • I was trying to call the event handler at private void ThisAddIn_Startup(object sender, System.EventArgs e)

    but could not get it to call correctly.

    So how do I call it from a new Inspector window?


    meaning, what is the syntex?
    • Edited by niyack Friday, December 7, 2012 6:11 PM
    Friday, December 7, 2012 6:10 PM
  • There is a problem with Outlook where handlers tend to get garbage collected if they are not added to objects that won't go out of scope (declare them at the class level) so you will need to declare the "inspectors" variable at the class level before using this code:

    class level definition: Outlook.Inspectors inspectors;

    Code for handler:

    inspectors = this.Application.Inspectors;
    inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);

    Inspectors_NewInspector is obviously the method that will be called by the handler, but you could do something like:

    void Inspectors_NewInspector(Outlook.Inspector Inspector)
    {
    if (Inspector.CurrentItem is Outlook.MailItem)
    {
    Outlook.MailItem mailItem = Inspector.CurrentItem;
    // Do whatever you want with the mailItem, check if it's a reply/reply all/forward, etc.
    }
    }


    Friday, December 7, 2012 6:24 PM
  • I tried the below yet I am not getting a response

    what I expect to get for now, is a message box but nothing happens

    Outlook.Inspectors inspectorsR;

            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                //Add items to new emails composed
                inspectors = this.Application.Inspectors;
                inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(srcOutlook.addNewMail);

                //Capture the send event and parse subject
                this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(srcOutlook.cleanSubjectOnItemSend);

    //Reply code

                inspectorsR = this.Application.Inspectors;
                inspectorsR.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(reply_NewInspector);
            }

     private void reply_NewInspector(Outlook.Inspector Inspector)
            {
                if (Inspector.CurrentItem is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem = Inspector.CurrentItem;

                    ((Outlook.ItemEvents_10_Event)mailItem).Reply += new Microsoft.Office.Interop.Outlook.ItemEvents_10_ReplyEventHandler(srcOutlook.AddinModule_Reply);
                }
            }

    public void AddinModule_Reply(object Response, ref bool Cancel)
            {
                Outlook.MailItem mail = Response as Outlook.MailItem;

                try
                {
                    MessageBox.Show("Got Here");
                }
                catch (System.Exception e)
                {
                    MessageBox.Show("ERROR: " + e.Message);
                }
                finally
                {
                    if (mail != null) { mail = null; }
                }
            }

    Friday, December 7, 2012 6:41 PM
  • I was suggesting eliminating the reply/forward handler all together. The inspector handler would let you know that a new inspector window(Compose,Reply,Reply All,Forward) has been opened, and give you a reference to the MailItem. You can then execute whatever code you would have been executing in the Reply/Forward handler inside the Inspector handler now.
    Friday, December 7, 2012 6:46 PM
  • ok, then how do I tell if it is a reply, Reply All,Forward in your method?

    there is a mailItem.Reply but that wants to create a new reply message
    • Edited by niyack Friday, December 7, 2012 6:53 PM
    Friday, December 7, 2012 6:52 PM
  • Not sure if there is a property on a MailItem to tell you if a given MailItem is a forward or reply or not, but you could do something like check the MailItem's subject.  If it starts with RE: it's reply, FW: for forward, etc..  There may be a more reliable solution I haven't done much research on it, but it seems like that would work for now.  Your original method just doesn't seem viable, you'd literally have to attach the .Reply or .Forward handler to every MailItem in the user's Inbox or Sent Mail folder for it to work.
    Friday, December 7, 2012 7:03 PM
  • ok, one last thing.

    I got code in place testing for the new, reply and so on, however then I try to write to the body of the reply, it writes to the main message instead. Would you know how to solve that one?

    Friday, December 7, 2012 7:49 PM
  • Checking the subject is about the only way. You can tell from ConversationIndex that a new message is part of a conversation and not the first member of the conversation.
     
    If you can find the preceding message in the thread by using ConversationIndex and ConversationTopic you could use Extended MAPI or Redemption to read the MAPI PR_LAST_VERB_EXECUTED property and see if the last verb was EXCHIVERB_REPLYTOALL, or EXCHIVERB_REPLYTOSENDER, or EXCHIVERB_FORWARD.
     
    See http://www.outlookcode.com/codedetail.aspx?id=1714 for an example of finding the parent message.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Brian Atkinson" <=?utf-8?B?QnJpYW4gQXRraW5zb24=?=> wrote in message news:e080106b-9762-4ca3-8b14-080f91a314f7...
    Not sure if there is a property on a MailItem to tell you if a given MailItem is a forward or reply or not, but you could do something like check the MailItem's subject.  If it starts with RE: it's reply, FW: for forward, etc..  There may be a more reliable solution I haven't done much research on it, but it seems like that would work for now.  Your original method just doesn't seem viable, you'd literally have to attach the .Reply or ..Forward handler to every MailItem in the user's Inbox or Sent Mail folder for it to work.

    Ken Slovak MVP - Outlook
    Monday, December 10, 2012 2:14 PM
    Moderator