.NET: Get all Outlook calendar items

前端 未结 11 1347
情书的邮戳
情书的邮戳 2020-12-02 09:41

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 requ

11条回答
  •  半阙折子戏
    2020-12-02 10:15

        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();
    
                       // get all date 
                DateTime first = new DateTime( item.Start.Hour, item.Start.Minute, 0);
                DateTime last = new DateTime();
                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());
            }
        }
    
    }
    

    it is working I try it but you need to add reference about Microsoft outlook

提交回复
热议问题