Bill Nielsen
Bill Nielsen

Reputation: 876

Cannot find a specific Outlook Calendar

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

Upvotes: 1

Views: 625

Answers (1)

Ken White
Ken White

Reputation: 125689

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;
    }
}

Upvotes: 1

Related Questions