views:

124

answers:

2

I have an application written in Delphi that adds / updates contacts in outlook. The problem I'm having is that if the contact has been deleted in Outlook, the code still finds the contact and updates it - and the contact still remains deleted. Is there a way I can determine if the contact is deleted or undelete the contact?

Roughly the code looks something like:

  OutlookApp := CreateOleObject('Outlook.Application');
  Mapi := OutlookApp.GetNameSpace('MAPI');

//.....
        try
          if ContactOutlookEntryID.AsString <> '' then
            aContact := Mapi.GetItemFromID(ContactOutlookEntryID.AsString);
        except
        end;
          //try to locate the contact if they have been synchro'd before
        if VarIsEmpty(aContact) then //if not found
          aContact := Contacts.Items.Add(2); //add a new contact to outlook
        aContact.LastName := ContactSurname.AsString;
//.....
+3  A: 

When contacts are deleted they are put in the Deleted Items folder. There is no other "deleted" state other than being in that folder. "Undeleting" is as simple as moving it back out.

There is a Move method on the ContactItem object that you can use to move it back to the default contact folder which you can get with the NameSpace.GetDefaultFolder method.

EDIT To determine if the contact is in the deleted items folder you can look at the Parent property which should return a MAPIFolder object. You can then compare its EntryID against the one returned by GetDefaultFolder(olFolderDeletedItems).

Josh Einstein
If the contact is removed from deleted items, I still have the same issue, but it gets me half way there - I'll see if I can work out how to determine if the contact is in deleted items - if they are I'll move them back to the default contact folder.
Alister
If the contact is truly deleted from the deleted items folder, I can't think of any possible way to get at it from the Outlook API. Perhaps it is being cached somehow.
Josh Einstein
I updated my answer with a tip on determining whether an item is in Deleted Items folder.
Josh Einstein
You may be holding a handle open to the deleted item thus you can get the item again. Restart outlook and try and see it there still
76mel
I seem to get errors when saving the contact after moving it from Deleted Items to the Contacts Folder - so I'm just giving up and creating a new contact - which works fine.
Alister
+1  A: 

Keep in mind that this is PST specific - the PST provider does not change the entry id when items are moved to different folders.

Dmitry Streblechenko (MVP) http://www.dimastr.com/ OutlookSpy - Outlook, CDO and MAPI Developer Tool