2008-09-18 8 views
26

특정 날짜의 특정 일정에서 모든 항목을 가져 오는 방법은 무엇입니까? 예를 들어 매주 월요일마다 반복되는 항목이있는 캘린더가 있다고 가정 해 보겠습니다..NET : 모든 Outlook 일정 항목 가져 오기

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

난 단지 한 항목을 가져 오기 ...

달력에서 모든 항목 (기본 항목 + 파생 항목)를 얻을 수있는 쉬운 방법이 있나요

:이 같은 모든 항목을 요청하면? 내 특정 상황에서 날짜 제한을 설정할 수는 있지만 모든 항목을 가져 오는 것이 좋습니다 (반복되는 항목은 시간 제한이 있습니다).

Microsoft Outlook 12 Object Library (Microsoft.Office.Interop.Outlook)을 사용하고 있습니다.

답변

13

되풀이 약속을 얻으려면 제한 또는 찾기를해야한다고 생각합니다. 그렇지 않으면 Outlook에서 확장되지 않습니다. 또한 시작 에 따라 IncludeRecurrences 앞에 정렬해야합니다.

+1

것은 우리가 성능이 크게 발생 양해하여 주시기 바랍니다 여기

은 VBA 예제입니다 일부 시스템/데이터에 대한 처벌 (일부 ms 대신 최대 30 초) 항목을 정렬 할 때 설정됩니다. –

30

나는이 문서를 공부했는데 이것이 내 결과 다 : 하드 코딩 된 한 달의 시간 제한을 두었지만 이것은 단지 예일 뿐이다.

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

    } 

위의 두 가지 답변을 보내 주셔서 감사합니다.

-3
calendarFolder = 
    mapiNamespace.GetDefaultFolder(
     Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar); 
+0

이것은 달력 폴더에 대한 참조를 제공합니다. 질문에 전혀 대답하지 않습니다. –

1

친구가 공유 폴더에 액세스해야하는 경우 친구를 수신자로 설정할 수 있습니다. 요구 사항 : 자신의 캘린더를 먼저 공유해야합니다.

// Set recepient 
Outlook.Recipient oRecip = (Outlook.Recipient)oNS.CreateRecipient("[email protected]"); 

// Get calendar folder 
Outlook.MAPIFolder oCalendar = oNS.GetSharedDefaultFolder(oRecip, Outlook.OlDefaultFolders.olFolderCalendar); 
+1

이 질문과 관련이없는 것으로 보입니다. –

7

나는 비슷한 코드를 작성하지만, 다음 내보내기 기능 발견 : 수동으로 되풀이 항목을 확장 할 필요가 없습니다

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"); 
1

합니다. IncludeRecurrences를 사용하여 앞에 정렬하도록하십시오.

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 
2

LinqPad 냈다 나를 위해 작동 :

//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

나는 또한 정렬의 순서를 뒤집어서 includerecurrences가 테스트 할 때 중요하지 않은 것으로 보입니다. YMMV하지만 최근에 이것을 사용했고 주어진 일 동안 중복 된 결과를 보냈습니다. 나의 최종 질의는 불과 며칠 동안이 아니라 6 개월 동안의 데이터에 대한 것이 었습니다. –

1
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

이 시도 :

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; 

    } 

}