共享加载项为Outlook 2007年捕获ReplyToAll事件 [英] Shared Add-ins for Outlook 2007 Capturing ReplyToAll Event

查看:206
本文介绍了共享加载项为Outlook 2007年捕获ReplyToAll事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS 2010和放大器;点NET Framework 2.0中。 我已经创建了Extensibility->共享加载项为Outlook项目。 我试图捕捉ReplyToAll事件它没有被解雇。 请看下面的code:

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方法

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");
    }

但被调用的Connect_ReplyAll方法 哪里不对 ?

But the Connect_ReplyAll method is been invoked What is Wrong ?

新的code这是工作,但该事件被注册

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);
        }                                 
    }

有一次,我注册全部答复事件中的MailItem如果相同的MailItem被选中,则此事件触发多次。这个问题是由使用上述code解决,但我得到新的错误,当我拆下事件 请帮我出

"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

我收到此错误

推荐答案

您希望提高该事件的COM对象必须是活的。您的code以上通过收件箱中的所有项目环(哎哟!为什么?),并使用在每个迭代从而扫除了previous值相同的变量。
要回复消息时,它首先需要选择,因此,你只能通过选定的项目(Explorer.Selection集合)需要循环。跟踪通过在Explorer.Selection collaction所有项目挂钩Explorer.SelectionChanged事件,在该事件处理程序,循环的选择和把它们放在自己的名单,其中,的MailItem> 名单。通过这种方式,对象将是活着,直到您删除它们FRM的列表。
更好,而不是使用名单,其中重要的是,;的MailItem> 列表,创建您自己的包装,存储的MailItem作为一个私有成员和挂钩的全部答复事件的包装。这样当事件触发时,你就会知道它的MailItem对象引发事件。该包装为每个选定的MailItem可以被存储在一个名单,其中,MyMailItemWrapper> 集合

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.

这篇关于共享加载项为Outlook 2007年捕获ReplyToAll事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆