1

I'm trying to find a specific Outlook calendar. I've looked at the instructions from this Outlook addin: Get elements from a selected calendar.

When I try to implement it with this code:

public static MAPIFolder GetTimeTrackingCalendar()
{
MAPIFolder result = null;
MAPIFolder calendars = (MAPIFolder)outlook.ActiveExplorer().Session.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
for (int i = 0; i < calendars.Folders.Count; i++)
{
    if (calendars.Folders[i].Name == "MyTimeTracker")
    {
        result = calendars.Folders[i];
        break;
    }
}
return result;

}

I get an error saying Array index out of bounds. Inspecting the calendars object, they're two folders but none support the Name property. Am I missing a cast?

Thanks, Bill N

Community
  • 1
  • 1
Bill Nielsen
  • 866
  • 2
  • 7
  • 18
  • 1
    It's been a while (that's why a comment and not an answer), but IIRC that the iteration should be 1-based instead of 0-based. I could be wrong, though. :) – Ken White Nov 30 '11 at 01:09
  • Thanks Ken. That was it. I wonder why when I inspect the calendar object and look at the two Folders, I don't see the Name property. – Bill Nielsen Nov 30 '11 at 13:41

1 Answers1

1

Just for future reference, Outlook (and the other Office automation objects) use 1-based indexes instead of 0 based. This is what causes the "array index out of bounds" error.

Changing the loop like this fixes it:

for (int i = 1; i <= calendars.Folders.Count; i++)
{
    if (calendars.Folders[i].Name == "MyTimeTracker")
    {
        result = calendars.Folders[i];
        break;
    }
}
Ken White
  • 123,280
  • 14
  • 225
  • 444