Unable to delete contact folder

Add-in Express™ Support Service
That's what is more important than anything else

Unable to delete contact folder
 
Karolis Ninzevi?ius




Posts: 4
Joined: 2008-08-12
Hello everybody,

I am trying to delete a customly created contact folder

I have read about this issue in this forum, but this is a new one and previous solutions doesn't work...

This is how I create it.

MAPIFolder defaultFolder = OutlookApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
if (defaultFolder == null) return;
MAPIFolder myFolder = defaultFolder.Folders.Add("MyFolder",OlDefaultFolders.olFolderContacts);


Then I programatically created couple hundred of contacts. Now I want to delete it. None of this works:

1. It throws an exception "You cannot close the mailbox that contains your calendar, contacts, and inbox"


OutlookApp.GetNamespace("MAPI").RemoveStore(myFolder); 


2. Does nothing - neither error, nor result


myFolder.Delete()


3. Throws exception "A folder with this name already exists. Use another name"


trashFolder = OutlookApp.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderDeletedItems);
myFolder.MoveTo(trashFolder);


Anyone knows why this is happening? Any ideas would be appreciated!

Best regards,
Karolis
Posted 12 Aug, 2008 15:15:49 Top
Karolis Ninzevi?ius




Posts: 4
Joined: 2008-08-12
Error occurs even while deleting this folder's items. Exception is thrown after half of items are deleted. E.g. If folder contains 400 contacts, error occurs while deleting 201th contact. If I run this code once again for other 200 contacts which have not been deleted, I get error while deleting 101st contact.

                        
int contCount = myFolder.Items.Count;
                        
for (int j = 0; j < contCount; j++)
{

   try
   {
      (myFolder.Items.Item(j + 1) as ContactItem).Delete();
   }
   catch (Exception exc)
   {
       ShowMessageError(exc.Message + exc.StackTrace);
   }
}
Posted 12 Aug, 2008 15:59:16 Top
David Thompson


Guest


Each time you delete an item, myFolder.Items.Count goes down by one...
Posted 12 Aug, 2008 16:13:26 Top
Karolis Ninzevi?ius




Posts: 4
Joined: 2008-08-12
Thanks, that was a stupid mistake :)

What about folder deletion issue? Do you have any clue?
Posted 12 Aug, 2008 16:24:04 Top
Karolis Ninzevi?ius




Posts: 4
Joined: 2008-08-12
Thanks for your help. For those who are stuck with this problem and still interested, here the solution, which is pretty odd.

If You try to delete your folder using Outlook (Edit -> Delete), nothing happens - You get the same result as myFolder.Delete()

You should rename it and then use Delete() and.. It's GONE!

Here is the code:


//Default contacts folder, usually "Contacts"
MAPIFolder defaultFolder = OutlookApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); 
if (defaultFolder == null) return;

//Make sure new folder has unique name
string renamedFolderName = myFolder.Name + "_" + myFolder.EntryID;

//Adding new folder
renamedFolder = defaultFolder.Folders.Add(renamedFolderName, 
OlDefaultFolders.olFolderContacts);

//Moving "undeletable" folder to the new location
myFolder.MoveTo(renamedFolder);

//Now You can see both of them. Delete new folder and they both are deleted
renamedFolder.Delete();


Don't forget to realese COM objects.

Have a good day.
Posted 13 Aug, 2008 06:34:47 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Karolis.

Thanks for the solution.
Posted 13 Aug, 2008 18:04:16 Top