如何捕获 OutlookContact.Write 事件?

发布于 2024-12-13 06:49:45 字数 1488 浏览 2 评论 0原文

我正在尝试使用 Application.ItemLoad 事件挂钩一个方法:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.ItemLoad += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemLoadEventHandler(Application_ItemLoad);
}

它将检查当前项目是否为 ContactItem。如果它是一个 ContactItem,它将检查属性 ContactItem.User4 是否包含值 xxx。如果 ContactItem.User4 包含值 xxx,它将通过 ContactItem.Write 事件挂钩另一个方法:

void Application_ItemLoad(object Item)
        {
            if (Item is Outlook.ContactItem)
            {
                Outlook.ContactItem contact = (Outlook.ContactItem)Item;
                System.Windows.Forms.MessageBox.Show("A new contact is loaded into memory");

                try
                {
                    string user4 = contact.User4;
                    bool isSynchronized =  user4 != null && user4.Contains("xxx");

                    if (isSynchronized)
                    {
                        contact.Write += propertyChangeHandler;
                    }
                }
                catch (Exception e)
                {
                    System.Windows.Forms.MessageBox.Show("An error occurred: "+e);
                }
            }
        }

现在的问题是,每当我尝试访问 ContactItem.User4 属性时,都会收到异常:

System.Runtime.InteropServices.COMException:该项目的属性和 不能在此事件过程中使用方法。

我应该怎么做才不会出现上述错误?

感谢您阅读我的长问题并期待您的建议。

I am trying to hook a method with Application.ItemLoad event:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.ItemLoad += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemLoadEventHandler(Application_ItemLoad);
}

Which would check if current item is ContactItem. If it is a ContactItem it would check if the property ContactItem.User4 contains value xxx. If ContactItem.User4 contains value xxx, it would hook another method with ContactItem.Write event:

void Application_ItemLoad(object Item)
        {
            if (Item is Outlook.ContactItem)
            {
                Outlook.ContactItem contact = (Outlook.ContactItem)Item;
                System.Windows.Forms.MessageBox.Show("A new contact is loaded into memory");

                try
                {
                    string user4 = contact.User4;
                    bool isSynchronized =  user4 != null && user4.Contains("xxx");

                    if (isSynchronized)
                    {
                        contact.Write += propertyChangeHandler;
                    }
                }
                catch (Exception e)
                {
                    System.Windows.Forms.MessageBox.Show("An error occurred: "+e);
                }
            }
        }

Now the problem is, whenever I try to access ContactItem.User4 property, I get an exception:

System.Runtime.InteropServices.COMException: The item's properties and
methods cannot be used inside this event procedure.

What should I do that I don't get the above error?

Thanks for reading my long question and looking forward to your suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舂唻埖巳落 2024-12-20 06:49:45

You need to use a different event. According to this post - the contents of the item are not yet loaded into memory. You should look at the Application.Inspectors event NewInspector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文