views:

4461

answers:

7

How can I get all items from a specific calendar (for a specific date). Lets say for instance that I have a calendar with a recurring item every Monday evening. When I request all items like this:

CalendarItems = CalendarFolder.Items;
CalendarItems.IncludeRecurrences = true;

I only get 1 item...

Is there an easy way to get all items (main item + derived items) from a calendar? In my specific situation it can be possible to set a date limit but it would be cool just to get all items (my recurring items are time limited themselves).

I'm using the Microsoft Outlook 12 Object library (Microsoft.Office.Interop.Outlook).

A: 

What api do you use to fetch those items? Because right now, we have no clue ;)

maybe this helps, but without more information I wouldn't know :s

Stormenet
+2  A: 

I believe that you must Restrict or Find in order to get recurring appointments, otherwise Outlook won't expand them. Also, you must Sort by Start before setting IncludeRecurrences.

Mark Brackett
+5  A: 

I've studied the docs and this is my result: I've put a time limit of one month hard-coded, but this is just an example.

    public void GetAllCalendarItems()
    {
        Microsoft.Office.Interop.Outlook.Application oApp = null;
        Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = null;
        Microsoft.Office.Interop.Outlook.MAPIFolder CalendarFolder = null;
        Microsoft.Office.Interop.Outlook.Items outlookCalendarItems = null;

        oApp = new Microsoft.Office.Interop.Outlook.Application();
        mapiNamespace = oApp.GetNamespace("MAPI"); ;
        CalendarFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);            outlookCalendarItems = CalendarFolder.Items;
        outlookCalendarItems.IncludeRecurrences = true;

        foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in outlookCalendarItems)
        {
            if (item.IsRecurring)
            {
                Microsoft.Office.Interop.Outlook.RecurrencePattern rp = item.GetRecurrencePattern();
                DateTime first = new DateTime(2008, 8, 31, item.Start.Hour, item.Start.Minute, 0);
                DateTime last = new DateTime(2008, 10, 1);
                Microsoft.Office.Interop.Outlook.AppointmentItem recur = null;



                for (DateTime cur = first; cur <= last; cur = cur.AddDays(1))
                {
                    try
                    {
                        recur = rp.GetOccurrence(cur);
                        MessageBox.Show(recur.Subject + " -> " + cur.ToLongDateString());
                    }
                    catch
                    { }
                }
            }
            else
            {
                MessageBox.Show(item.Subject + " -> " + item.Start.ToLongDateString());
            }
        }

    }

Thanks for the two answers above!

A: 

Awesome! Thanks for the help on this. I was having trouble figuring out how to do this and your idea of cycling forward through the recurrence pattern did the trick!

Eric Kramer Moon Song Gallery

A: 

Can you tell me how to use GetFolder() and where I can get it?

fixed the original code to include it (as seen in uygar's comment)
Leon Bambrick
A: 
I tried the following code:

------------------------------- C U T - H E R E -------------------------------

Microsoft.Office.Interop.Outlook.Application oApp = null;
Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = null;
Microsoft.Office.Interop.Outlook.MAPIFolder calendarFolder = null;

oApp = new Microsoft.Office.Interop.Outlook.Application();
mapiNamespace = oApp.GetNamespace("MAPI");
calendarFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);

foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in calendarFolder.Items)
{
    System.Console.WriteLine(item.Location);
}

------------------------------- C U T - H E R E -------------------------------

But get an exception at the start of the foreach loop:

System.InvalidCastException was unhandled

Message="Unable to cast COM object of
type 'System.__ComObject' to interface
type 'Microsoft.Office.Interop.Outlook.AppointmentItem'.

This operation failed because the QueryInterface call on the COM component
for the interface with IID '{00063033-0000-0000-C000-000000000046}'
failed due to the following error: No such interface supported
(Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

------------------------------- C U T - H E R E -------------------------------

For whatever it's worth, I did put into AsssmelbyInfo.cs:

[assembly: ComVisible(true)]

but I still get the exception.

Any ideas, please?
just because it is your default calendar folder doesn't mean it's full of AppointmentItems. calendarFolder.Items.OfType<AppointmentItem>()of foreach objectand check before the cast!
HumerGu
A: 

calendarFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);

uygar