Exception Removing/Deleting Appointment??

C

Crash

Windows XP SP2
C# .NET v1.1
Outlook 2003 {via Office 11.0 PIA}

I'm manipulating Outlook's calendar via OLE automation from my C#
application. I would like to iterate through the calendar items
collection and delete appointments from the items collection based on
some business rules. However after I delete/remove items I am getting
"index out of range" and/or "object has been moved or deleted" errors.
Also, sometimes it seems that the item collection's Count property does
not acknowledge that I have deleted something during a loop iteration -
has anybody seen this behavior?

Question: What is the correct & proper way to delete a calendar item
while iterating the items collection? Is it AppointmentItem.Delete()
or is it Items.Remove(index)??

Pseudo code, note that I don't use a foreach loop because I need to
modify the collection:

int i = 1;
while (i <= items.Count)
{
AppointmentItem ai = (AppointmentItem) items;

if (<<my business rules>>)
{
ai.Delete(); // --> or items.Remove(i) ????
}
else
{
<<my other action>>
++i;
}
}
 
K

Ken Slovak - [MVP - Outlook]

Use a down counting loop or a Do loop that tests for any items still
remaining.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top