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.

有帮助吗?

解决方案

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--)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top