So, essentially, what I'm trying to do is delete all appointments in Outlook that match a certain pattern (it's a simple one so there's no need for a Regex
).
So, I use the following code to retrieve every item in the calendar using interop.
Outlook.Application outlook = new Outlook.Application();
Outlook.NameSpace mapiNameSpace = outlook.GetNamespace("MAPI");
Outlook.MAPIFolder calendarFolder = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
Outlook.Items outlookCalendarItems = calendarFolder.Items;
And then the following code to iterate over and delete them:
foreach (Outlook.AppointmentItem appointment in outlookCalendarItems)
{
if (appointment.Subject.Contains("On Call: Regions:"))
{
appointment.Delete();
}
}
However, for some reason some of the items seem to get deleted and some of them get missed with no obvious pattern to why. The Subject
of each appointment is generated in the same way for each appointment (in a different part of the application):
appt.Subject = $"On Call: {appointment.Region}";
Where appointment.Region
is generated using:
Region = $"Regions: {regions.Aggregate((x, y) => x + " & " + y)}"
Which results in:
On Call: Regions: 6 & 7
On Call: Regions: NS
etc...
So if it's working for some, it should be working for all.
Any ideas?