简体   繁体   中英

Find Outlook AppointmentItem for ContactItem

I am creating ContactItem from code using Microsoft.Office.Interop.Outlook . This works absolutely fine. If I assign a the property Birthday , an corresponding AppointmentItem is created automatically. This is also fine.

But now when I delete the ContactItem , the AppointmentItem stays. This is obviously not what I was going for.

localContactToDelete.Delete();

Is there a way to retrieve the associated AppointmentItem in order to delete it manually?

I read that it should be possible (see below), however I do not find the propoerties or whatever.

http://social.msdn.microsoft.com/Forums/office/en-US/6b481f74-8422-46d4-90a9-a5860dcb98b5/to-avoid-automatic-birthday-calendar-event-creation-when-creating-contact?forum=outlookdev

"I do not find the propoerties or whatever"? Where did you look? Take a look at a contact with the birthday/anniversary properties set using OutlookSpy (click IMessage) - the entry id of the birthday appointment is stored in a named property with the DASL name of http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/804D0102 . Fo the anniversary, use http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/804E0102 .

Thanks to Dmitry's tip for using OutlookSpy to identify the NameSchema of the needed property I came up with the following code. It's definitely not ready, but at least works for the moment. Any suggestions welcome.

Microsoft.Office.Interop.Outlook.Application outlookObject = new Microsoft.Office.Interop.Outlook.Application();

MAPIFolder contactFolder = outlookObject.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
Items contacts = contactFolder.Items;

Then comes the stuff selecting the contact, eg a foreach.

ContactItem contact;

PropertyAccessor pa = contact.PropertyAccessor;
Byte[] ba = pa.GetProperty("http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/804D0102");
string birthdayAppointmentItemID = BitConverter.ToString(ba).Replace("-", string.Empty);

NameSpace ns = outlookObject.GetNamespace("MAPI");
AppointmentItem birthdayAppointmentItem = ns.GetItemFromID(birthdayAppointmentItemID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM