Reputation: 15623
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.
Upvotes: 4
Views: 1552
Reputation: 31641
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
.
Upvotes: 2