.NET: Get all Outlook calendar items

前端 未结 11 1312
情书的邮戳
情书的邮戳 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:07
    calendarFolder = 
        mapiNamespace.GetDefaultFolder(
            Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
    
    0 讨论(0)
  • 2020-12-02 10:11
    public void GetAllCalendarItems()
            {
                DataTable sample = new DataTable(); //Sample Data
                sample.Columns.Add("Subject", typeof(string));
                sample.Columns.Add("Location", typeof(string));
                sample.Columns.Add("StartTime", typeof(DateTime));
                sample.Columns.Add("EndTime", typeof(DateTime));
                sample.Columns.Add("StartDate", typeof(DateTime));
                sample.Columns.Add("EndDate", typeof(DateTime));
                sample.Columns.Add("AllDayEvent", typeof(bool));
                sample.Columns.Add("Body", typeof(string));
    
    
                listViewContacts.Items.Clear();
                oApp = new Outlook.Application();
                oNS = oApp.GetNamespace("MAPI");
                oCalenderFolder = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
                outlookCalendarItems = oCalenderFolder.Items;
                outlookCalendarItems.IncludeRecurrences = true;
               // DataTable sample = new DataTable();
                foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in outlookCalendarItems)
                {
                    DataRow row = sample.NewRow();
                    row["Subject"] = item.Subject;
                    row["Location"] = item.Location;
                    row["StartTime"] = item.Start.TimeOfDay.ToString();
                    row["EndTime"] = item.End.TimeOfDay.ToString();
                    row["StartDate"] = item.Start.Date;
                    row["EndDate"] = item.End.Date;
                    row["AllDayEvent"] = item.AllDayEvent;
                    row["Body"] = item.Body;
                    sample.Rows.Add(row);
                }
                sample.AcceptChanges();
                foreach (DataRow dr in sample.Rows)
                    {
                        ListViewItem lvi = new ListViewItem(dr["Subject"].ToString());
    
                        lvi.SubItems.Add(dr["Location"].ToString());
                        lvi.SubItems.Add(dr["StartTime"].ToString());
                        lvi.SubItems.Add(dr["EndTime"].ToString());
                        lvi.SubItems.Add(dr["StartDate"].ToString());
                        lvi.SubItems.Add(dr["EndDate"].ToString());
                        lvi.SubItems.Add(dr["AllDayEvent"].ToString());
                        lvi.SubItems.Add(dr["Body"].ToString());
    
    
    
                        this.listViewContacts.Items.Add(lvi);
                    }
                oApp = null;
                oNS = null;
    
            }
    
    0 讨论(0)
  • 2020-12-02 10:14

    I found this article very useful: https://docs.microsoft.com/en-us/office/client-developer/outlook/pia/how-to-search-and-obtain-appointments-in-a-time-range

    It demonstrates how to get calendar entries in a specified time range. It worked for me. Here is the source code from the article for your convenience :)

    using Outlook = Microsoft.Office.Interop.Outlook;
    
    private void DemoAppointmentsInRange()
    {
        Outlook.Folder calFolder = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
            as Outlook.Folder;
        DateTime start = DateTime.Now;
        DateTime end = start.AddDays(5);
        Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end);
        if (rangeAppts != null)
        {
            foreach (Outlook.AppointmentItem appt in rangeAppts)
            {
                 Debug.WriteLine("Subject: " + appt.Subject 
                     + " Start: " + appt.Start.ToString("g"));
            }
        }
    }
    
    /// <summary>
    /// Get recurring appointments in date range.
    /// </summary>
    /// <param name="folder"></param>
    /// <param name="startTime"></param>
    /// <param name="endTime"></param>
    /// <returns>Outlook.Items</returns>
    private Outlook.Items GetAppointmentsInRange(
        Outlook.Folder folder, DateTime startTime, DateTime endTime)
    {
        string filter = "[Start] >= '"
            + startTime.ToString("g")
            + "' AND [End] <= '"
            + endTime.ToString("g") + "'";
        Debug.WriteLine(filter);
        try
        {
            Outlook.Items calItems = folder.Items;
            calItems.IncludeRecurrences = true;
            calItems.Sort("[Start]", Type.Missing);
            Outlook.Items restrictItems = calItems.Restrict(filter);
            if (restrictItems.Count > 0)
            {
                return restrictItems;
            }
            else
            {
                return null;
            }
        }
        catch { return null; }
     }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-02 10:16

    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.

    0 讨论(0)
提交回复
热议问题