Thank you for the reply! Clean exit of Outlook is such a struggle. It looks
like this exception starts happening together with a warning in the right
lower corner of Outlook window:
"A data file did not close properly the last time it was used and is being
checked for problems. performance might be affected while the check is in
progress."
Here the method I use, which has some debugging/troubleshooting parts:
public void DeleteAllEmailsInFolder(MAPIFolder mpf) {
Trace.TraceInformation("Deleting items in folder \"{0}\"",
mpf.FullFolderPath);
// Increments delay between successive deletions, in millisecs.
int timeStep = 100;
// Limits the maximum delay, in millisecs.
int timeOut = 10000;
// Logon.
NameSpace outlookNameSpace = myOutlookApp.GetNamespace("MAPI");
// Determine how many items in folder.
int totalNumberOfItemsInFolderToDelete = mpf.Items.Count;
if (totalNumberOfItemsInFolderToDelete > 0) {
int mCounter = 0; // deleted mail item counter
int rCounter = 0; // deleted report item counter
int aCounter = 0; // deleted appointment item counter
// Iterate thru all items and call the delete method on
emails.
for (int i = totalNumberOfItemsInFolderToDelete;
i >= 1; i--) {
Object Item = mpf.Items;
if (Information.TypeName(Item) ==
"MailItem") {
int beforeDelete = mpf.Items.Count;
MailItem outlookMailItem = (MailItem)Item;
outlookMailItem.Delete();
//@ A delay for processing deletion
int timer = 0;
while (mpf.Items.Count != (beforeDelete - 1)) {
Thread.Sleep(timeStep);
timer += timeStep;
if (timer >= timeOut) {
Trace.TraceError(
"mDeletion was not completed within " +
"{0} millisecs.", timeOut);
break;
}
}
if (timer < timeOut) {
Trace.TraceInformation(
"mDeletion completed within {0}
millisecs.",
timer);
}
mCounter++;
continue;
}
if (Information.TypeName(Item) ==
"ReportItem") {
int beforeDelete = mpf.Items.Count;
ReportItem outlookReportItem = (ReportItem)Item;
outlookReportItem.Delete();
//@ A delay for processing deletion
int timer = 0;
while (mpf.Items.Count != (beforeDelete - 1)) {
Thread.Sleep(timeStep);
timer += timeStep;
if (timer >= timeOut) {
Trace.TraceError(
"rDeletion was not completed within " +
"{0} millisecs.", timeOut);
break;
}
}
if (timer < timeOut) {
Trace.TraceInformation(
"rDeletion completed within {0}
millisecs.",
timer);
}
rCounter++;
continue;
}
if (Information.TypeName(Item) ==
"AppointmentItem") {
int beforeDelete = mpf.Items.Count;
AppointmentItem outlookAppointmentItem =
(AppointmentItem)Item;
outlookAppointmentItem.Delete();
//@ A delay for processing deletion
int timer = 0;
while (mpf.Items.Count != (beforeDelete - 1)) {
Thread.Sleep(timeStep);
timer += timeStep;
if (timer >= timeOut) {
Trace.TraceError(
"aDeletion was not completed within " +
"{0} millisecs.", timeOut);
break;
}
}
if (timer < timeOut) {
Trace.TraceInformation(
"aDeletion completed within {0}
millisecs.",
timer);
}
aCounter++;
}
}
int totalDeleted = mCounter + rCounter + aCounter;
Trace.TraceInformation("{0} items have been deleted in " +
"\"{1}\" folder.", totalDeleted, mpf.FullFolderPath);
} else {
Trace.TraceInformation("There are no items to " +
"delete in \"{0}\"", mpf.FullFolderPath);
}
}