Shared Add-ins for Outlook 2007 Capturing ReplyToAll Event

南楼画角 提交于 2019-12-02 16:18:21

问题


I am using VS 2010 & Dot Net Framework 2.0. I have created a project in Extensibility->Shared Add-ins for Outlook. I am trying to capture ReplyToAll Event it is not getting fired. Please look the below code:

OnConnection Method

inspectors = applicationObject.Inspectors;                        
inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(inspectors_NewInspector);


void inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
    {
        mailItem = null;
        try
        {
            Outlook.NameSpace ns = Inspector.Session;
            Outlook.MAPIFolder inbox = ns.GetDefaultFolder(
              Outlook.OlDefaultFolders.olFolderInbox);

            foreach (object o in inbox.Items)
            {
                mailItem = o as Outlook.MailItem;
                if (mailItem != null)
                {
                    break;
                }
            }
            if (mailItem == null)
            {
                MessageBox.Show("Couldn't find a mail item.");
            }
            else
            {
                ((Outlook.ItemEvents_10_Event)mailItem).ReplyAll += new
                    Outlook.ItemEvents_10_ReplyAllEventHandler(Connect_ReplyAll);
            }                              
        }
        catch (Exception ex)
        {
            MessageBox.Show("asdgh"+ex.StackTrace);
        }        
    }


void Connect_ReplyAll(object Response, ref bool Cancel)
    {
        MessageBox.Show(Response+"Hello You have Clikced ReplyTOAll");
    }

But the Connect_ReplyAll method is been invoked What is Wrong ?

The new Code which is working but the event is registered

public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
    {
        try
        {
             applicationObject = (Outlook.Application)application;
            if (connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
            {
                OnStartupComplete(ref custom);
            }
            addInInstance = addInInst;
            inspectors = applicationObject.Inspectors;
            explorer = applicationObject.Explorers.Application.ActiveExplorer();

            explorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(explorer_SelectionChange);                                                
            inspectors.NewInspector += new 
                Outlook.InspectorsEvents_NewInspectorEventHandler(inspectors_NewInspector);               
        }
        catch(Exception ex)
        {
            MessageBox.Show(""+ex.StackTrace);
        }
        //((Microsoft.Office.Interop.Outlook.ItemEvents_10_Event)mailItem).Reply += new Microsoft.Office.Interop.Outlook.ItemEvents_10_ReplyEventHandler(ReplyToAllEvent);
    }

void explorer_SelectionChange()
    {
        try
        {
            Outlook.MailItem mailExplorer=null;
            mailTO = "";
            mailCC = "";
            mailBCC = "";
            foreach (object selectedItem in explorer.Selection)
            {
                mailExplorer = selectedItem as Outlook.MailItem;
                //MessageBox.Show("" + mailItem.EntryID.ToString());
                break;
            }               
            if (mailExplorer != null)
            {
                if (selectedItems.Contains(mailExplorer.EntryID.ToString()))
                {
                    selectedItems.Remove(mailExplorer.EntryID);
                    ((Outlook.ItemEvents_10_Event)mailExplorer).ReplyAll -= new Outlook.ItemEvents_10_ReplyAllEventHandler(Connect_ReplyAll);
                    ((Outlook.ItemEvents_10_Event)mailExplorer).Reply -= new Outlook.ItemEvents_10_ReplyEventHandler(Connect_Reply);                        
                }                    
                ((Outlook.ItemEvents_10_Event)mailExplorer).ReplyAll +=
                    new Outlook.ItemEvents_10_ReplyAllEventHandler(Connect_ReplyAll);
                ((Outlook.ItemEvents_10_Event)mailExplorer).Reply +=
                    new Outlook.ItemEvents_10_ReplyEventHandler(Connect_Reply);
                selectedItems.Add(mailExplorer.EntryID);
                mailTO = mailExplorer.To;
                mailCC = mailExplorer.CC;
                mailBCC = mailExplorer.BCC;
            }
        }
        catch(Exception ex)
        {                
            MessageBox.Show(""+ex.StackTrace);
        }                                 
    }

"Once I register the mailitem with ReplyAll Event If the same mailitem is selected then the event fires multiple times." this issue is resolved by using the above code but i am getting new error when i Detach the Event Please help me Out

I am getting this error


回答1:


The COM object that you expect to raise the event needs to be alive. Your code above loops through all items in the Inbox (ouch! why?) and uses the same variable on each iteration thus wiping out the previous value.
To reply to a message, it needs to be selected first, thus you only need to loop through the selected items (Explorer.Selection collection). Track the selection by hooking the Explorer.SelectionChanged event, in that event handler, loop through all items in the Explorer.Selection collaction and put them in your own List<MailItem> list. This way the objects will be alive until you remove them frm the list.
Better than that, instead of using List<MailItem> list, create your own wrapper that stores MailItem as a private member and hook up the ReplyAll event in that wrapper. This way when the event fires, your will know which MailItem object raised the event. The wrappers for each selected MailItem can then be stored in a List<MyMailItemWrapper> collection.



来源:https://stackoverflow.com/questions/14911177/shared-add-ins-for-outlook-2007-capturing-replytoall-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!