如何捕获 OutlookContact.Write 事件?
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用不同的事件。根据这篇文章 - 该项目的内容尚未加载到内存中。您应该查看
Application.Inspectors
事件NewInspector
。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
eventNewInspector
.