0

我正在使用 VS 2010 和 Dot Net Framework 2.0。我在 Extensibility->Shared Add-ins for Outlook 中创建了一个项目。我正在尝试捕获没有被解雇的 ReplyToAll 事件。请看下面的代码:

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 方法被调用了有什么问题吗?

正在运行但事件已注册的新代码

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

“一旦我使用 ReplyAll 事件注册了邮件项,如果选择了相同的邮件项,则该事件会触发多次。” 使用上面的代码解决了这个问题,但是当我分离事件时出现新错误请帮助我

我收到此错误

4

1 回答 1

2

您希望引发事件的 COM 对象需要处于活动状态。上面的代码循环遍历收件箱中的所有项目(哎哟!为什么?)并在每次迭代中使用相同的变量,从而消除先前的值。
要回复一条消息,首先需要选择它,因此您只需要遍历选定的项目(Explorer.Selection 集合)。通过挂钩 Explorer.SelectionChanged 事件来跟踪选择,在该事件处理程序中,循环浏览 Explorer.Selection 集合中的所有项目并将它们放入您自己的List<MailItem>列表中。这样,对象将一直存在,直到您将它们从列表中删除。
比这更好,而不是使用List<MailItem>列表,创建您自己的包装器,将 MailItem 存储为私有成员,并在该包装器中连接 ReplyAll 事件。这样,当事件触发时,您将知道哪个 MailItem 对象引发了事件。然后可以将每个选定 MailItem 的包装器存储在一个List<MyMailItemWrapper>集合中。

于 2013-02-16T19:15:56.530 回答