Domanda

I am facing a strange problem - all the Outlook folders are not being deleted.

 using OL = Microsoft.Office.Interop.Outlook;

 OL.NameSpace olNamespace = Globals.ThisAddIn.Application.GetNamespace("MAPI");
 OL.MAPIFolder olInboxFolder = olNamespace.Session.GetDefaultFolder(OL.OlDefaultFolders.olFolderInbox);

 foreach (OL .MAPIFolder folder in olInboxFolder .Folders)
 {
      folder.Delete();
 }

The above code deletes 4 out of the 7 folders. All the folders are empty, and the delete operation is being performed on the same PST.

È stato utile?

Soluzione

Wghen yo udelete folders, you change the number of items in the collection. Do not use the foreach loops if you delete the items. Use a down for loop:

for (int i = olInboxFolder.Folders.Count; i >= 1; i--)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top