0

I can add a contact to a address book but for some reason I can't remove it. The code I'm executing is as follows.

String abName = "Name ofthe targetted address book";
Outlook.Folder addressBook;
if (targetFolder.Folders.OfType<Outlook.Folder>().Any(element
  => element.Name == abName))
  addressBook = targetFolder.Folders[abName] as Outlook.Folder;
else
  addressBook = targetFolder.Folders.Add(
    abName, Outlook.OlDefaultFolders.olFolderContacts) as Outlook.Folder;
addressBook.ShowAsOutlookAB = true;

for (int i = addressBook.Items.Count - 1; i >= 0; i--)
  if (!stringList.Any(element 
    => element == addressBook.Items.OfType<Outlook.ContactItem>()
      .ToList()[i].Email1Address))
    addressBook.Items.OfType<Outlook.ContactItem>().ToList().RemoveAt(i);

The fetching of the address book works and the matching for strings too. I get into the RemoveAt line for the exactly correct contacts. There's no error or other message when I execute the removal. Still, the contact list remain unaffected.

  1. Why?
  2. What can I do to actually remove the contacts?

I suspect that I may be working on a copy of the actual list containing the contacts. The problem is that if I don't create a List, I'm not sure how to alter the list of contacts.

So, the most helpful answer would shed some light on how to alter addressBook (or perhaps addressBook.Items) given certain condition. E.g., say that we'd like to remove all the contants the name of whom starts with the letter "Q".

At this moment I can only think of a super ugly work-around and it's so rectum-ugly that I don't even mention it here. Really ugly...

2 Answers2

1

You ae not removing an Outlook contact. You are removing an OUtlook object from your own List object.

You need to call ContactItem.Delete.

As a side note, do not use multiple dot notation when working with COM objects, especially in a loop - you will receive a brand new COM object for each dot.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • 1
    I had a similar issue a few months ago and didn't really got it right in a nice way. I recall that removing the members of an address book only move them to another folder (*Deleted*, I think). Is there a better way to remove stuff in Outlook? As in - remove-remove? – Konrad Viltersten Jan 30 '13 at 22:55
  • 1
    You can then open the item in the Deleted Items folder (finding the right one might be tricky) and delete it again. Or you can use Redemption - its RDOMail.Delete method takes an enum which lets you specify how the message must be deleted: moved to Deleted Items folder, deleted, or hard deleted (Exchange specific - the item will unrecoverable). – Dmitry Streblechenko Jan 31 '13 at 17:04
  • Are you sure? I recall that the deletion from *Deleted Items* didn't work. And the *Redemption* - is it a class in .NET? Some customers are sensitive to installing third party products... – Konrad Viltersten Jan 31 '13 at 23:57
  • 1
    Yes, try an experiment - make sure Deleted Items folder is empty, call ContactItem.Delete (it will be moved to the Deleted Items folder), then call Application.Session.GetDefaultFolder(olFolderDeletedItems).Items.Item(1).Delete. Of course it will be more complicated if you have more than one item in the Deleted Items folder. Redemption is an unmanaged COM library accessible in any language, any .Net language included - http://www.dimastr.com/redemption. – Dmitry Streblechenko Feb 01 '13 at 21:25
1

Here is a solution

    private void ClearContact(Outlook.Application outlookApplication)
    {
        Outlook.MAPIFolder contactFolder = outlookApplication.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
        int total = contactFolder.Items.Count;
        while (total > 0)
        {
            // first index number is 1 not 0
            var contact = (Outlook.ContactItem)contactFolder.Items[1];
            contact.Delete();
            total = contactFolder.Items.Count;
        }
    }

I use netoffice outlook api http://netoffice.codeplex.com/wikipage?title=Outlook_Example05 And use while loop to delete all contact

Dung Le
  • 86
  • 3