.NET: Get all Outlook calendar items

前端 未结 11 1311
情书的邮戳
情书的邮戳 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 09:59

    Try this:

        public List<AdxCalendarItem> GetAllCalendarItems()
        {
            Outlook.Application OutlookApp = new Outlook.Application();
            List<AdxCalendarItem> result = new List<AdxCalendarItem>();
                Outlook._NameSpace session = OutlookApp.Session;
                if (session != null)
                    try
                    {
                        object stores = session.GetType().InvokeMember("Stores", BindingFlags.GetProperty, null, session, null);
                        if (stores != null)
                            try
                            {
                                int count = (int)stores.GetType().InvokeMember("Count", BindingFlags.GetProperty, null, stores, null);
                                for (int i = 1; i <= count; i++)
                                {
                                    object store = stores.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, stores, new object[] { i });
                                    if (store != null)
                                        try
                                        {
                                            Outlook.MAPIFolder calendar = null;
                                            try
                                            {
                                                calendar = (Outlook.MAPIFolder)store.GetType().InvokeMember("GetDefaultFolder", BindingFlags.GetProperty, null, store, new object[] { Outlook.OlDefaultFolders.olFolderCalendar });
                                            }
                                            catch
                                            {
                                                continue;
                                            }
                                            if (calendar != null)
                                                try
                                                {
                                                    Outlook.Folders folders = calendar.Folders;
                                                    try
                                                    {
                                                        Outlook.MAPIFolder subfolder = null;
                                                        for (int j = 1; j < folders.Count + 1; j++)
                                                        {
                                                            subfolder = folders[j];
                                                            try
                                                            {
                                                               // add subfolder items
                                                                result.AddRange(GetAppointmentItems(subfolder));
                                                            }
                                                            finally
                                                            { if (subfolder != null) Marshal.ReleaseComObject(subfolder); }
                                                        }
                                                    }
                                                    finally
                                                    { if (folders != null) Marshal.ReleaseComObject(folders); }
                                                    // add root items
                                                    result.AddRange(GetAppointmentItems(calendar));
                                                }
                                                finally { Marshal.ReleaseComObject(calendar); }
                                        }
                                        finally { Marshal.ReleaseComObject(store); }
                                }
                            }
                            finally { Marshal.ReleaseComObject(stores); }
                    }
                    finally { Marshal.ReleaseComObject(session); }
            return result;
        }
    
        List<AdxCalendarItem> GetAppointmentItems(Outlook.MAPIFolder calendarFolder)
        {
            List<AdxCalendarItem> result = new List<AdxCalendarItem>();
            Outlook.Items calendarItems = calendarFolder.Items;
            try
            {
                calendarItems.IncludeRecurrences = true;
                Outlook.AppointmentItem appointment = null;
                for (int j = 1; j < calendarItems.Count + 1; j++)
                {
                    appointment = calendarItems[j] as Outlook.AppointmentItem;
                    try
                    {
                        AdxCalendarItem item = new AdxCalendarItem(
                            calendarFolder.Name,
                            appointment.Subject,
                                       appointment.Location,
                                       appointment.Start,
                                       appointment.End,
                                       appointment.Start.Date,
                                       appointment.End.Date,
                                       appointment.AllDayEvent,
                                       appointment.Body);
                        result.Add(item);
                    }
                    finally
                    {
                        { Marshal.ReleaseComObject(appointment); }
                    }
                }
            }
            finally { Marshal.ReleaseComObject(calendarItems); }
            return result;
        }
    }
    
    public class AdxCalendarItem
    {
        public string CalendarName;
        public string Subject;
        public string Location;
        public DateTime StartTime;
        public DateTime EndTime;
        public DateTime StartDate;
        public DateTime EndDate;
        public bool AllDayEvent;
        public string Body;
    
        public AdxCalendarItem(string CalendarName, string Subject, string Location, DateTime StartTime, DateTime EndTime,
                                DateTime StartDate, DateTime EndDate, bool AllDayEvent, string Body)
        {
            this.CalendarName = CalendarName;
            this.Subject = Subject;
            this.Location = Location;
            this.StartTime = StartTime;
            this.EndTime = EndTime;
            this.StartDate = StartDate;
            this.EndDate = EndDate;
            this.AllDayEvent = AllDayEvent;
            this.Body = Body;
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-02 10:00

    There is no need to expand recurring items manually. Just ensure you sort the items before using IncludeRecurrences.

    Here is VBA example:

    tdystart = VBA.Format(#8/1/2012#, "Short Date")
    tdyend = VBA.Format(#8/31/2012#, "Short Date")
    
    Dim folder As MAPIFolder
    Set appointments = folder.Items
    
    appointments.Sort "[Start]" ' <-- !!! Sort is a MUST
    appointments.IncludeRecurrences = True ' <-- This will expand reccurent items
    
    Set app = appointments.Find("[Start] >= """ & tdystart & """ and [Start] <= """ & tdyend & """")
    
    While TypeName(app) <> "Nothing"
       MsgBox app.Start & " " & app.Subject
       Set app = appointments.FindNext
    Wend
    
    0 讨论(0)
  • 2020-12-02 10:03

    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());
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-02 10:05

    LinqPad snipped that works for me:

    //using Microsoft.Office.Interop.Outlook
    Application a = new Application();
    Items i = a.Session.GetDefaultFolder(OlDefaultFolders.olFolderCalendar).Items;
    i.IncludeRecurrences = true;
    i.Sort("[Start]");
    i = i.Restrict(
        "[Start] >= '10/1/2013 12:00 AM' AND [End] < '10/3/2013 12:00 AM'");
    
    
    var r =
        from ai in i.Cast<AppointmentItem>()
        select new {
            ai.Categories,
            ai.Start,
            ai.Duration
            };
    r.Dump();
    
    0 讨论(0)
  • 2020-12-02 10:05

    If you need want to access the shared folder from your friend, then you can set your friend as the recipient. Requirement: his calendar must be shared first.

    // Set recepient
    Outlook.Recipient oRecip = (Outlook.Recipient)oNS.CreateRecipient("abc@yourmail.com");
    
    // Get calendar folder 
    Outlook.MAPIFolder oCalendar = oNS.GetSharedDefaultFolder(oRecip, Outlook.OlDefaultFolders.olFolderCalendar);
    
    0 讨论(0)
  • 2020-12-02 10:07

    I wrote similar code, but then found the export functionality:

    Application outlook;
    NameSpace OutlookNS;
    
    outlook = new ApplicationClass();
    OutlookNS = outlook.GetNamespace("MAPI");
    
    MAPIFolder f = OutlookNS.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
    
    CalendarSharing cs = f.GetCalendarExporter();
    cs.CalendarDetail = OlCalendarDetail.olFullDetails;
    cs.StartDate = new DateTime(2011, 11, 1);
    cs.EndDate = new DateTime(2011, 12, 31);
    cs.SaveAsICal("c:\\temp\\cal.ics");
    
    0 讨论(0)
提交回复
热议问题