2008-09-18 19 views
26

मैं किसी विशिष्ट कैलेंडर (किसी विशिष्ट दिनांक के लिए) से सभी आइटम कैसे प्राप्त कर सकता हूं। उदाहरण के लिए कहें कि मेरे पास हर सोमवार की शाम को आवर्ती आइटम वाला कैलेंडर होता है।.NET: सभी Outlook कैलेंडर आइटम प्राप्त करें

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

मैं केवल 1 आइटम मिलता है ...

वहाँ एक आसान तरीका एक कैलेंडर से सभी आइटम (मुख्य आइटम + व्युत्पन्न आइटम) प्राप्त करने के लिए है: मैं इस तरह सभी वस्तुओं का अनुरोध करते हैं? मेरी विशिष्ट स्थिति में दिनांक सीमा निर्धारित करना संभव हो सकता है लेकिन सभी आइटम प्राप्त करने के लिए यह अच्छा होगा (मेरे पुनरावर्ती आइटम स्वयं सीमित हैं)।

मैं माइक्रोसॉफ्ट आउटलुक 12 ऑब्जेक्ट लाइब्रेरी (माइक्रोसॉफ्ट.ऑफिस.इंटरोप.ऑटlook) का उपयोग कर रहा हूं।

उत्तर

13

मुझे विश्वास है कि आवर्ती नियुक्तियों को प्राप्त करने के लिए आपको प्रतिबंधित या ढूंढना होगा, अन्यथा Outlook उन्हें विस्तृत नहीं करेगा। इसके अलावा, आपको सेटिंग से पहले प्रारंभ करना द्वारा क्रमबद्ध करना होगा।

+1

कृपया ध्यान रखें कि हम महत्वपूर्ण प्रदर्शन का सामना करना पड़ा हो

यहाँ VBA उदाहरण है कुछ सिस्टम/डेटा पर जुर्माना (कुछ एमएस के बजाय 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

मैन्युअल आवर्ती आइटम विस्तार करने के लिए कोई जरूरत नहीं है। बस सुनिश्चित करें कि आप से पहले आइटम को सॉर्ट करें।

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

मैं यह भी ध्यान दूंगा कि सॉर्ट और समेकन के क्रम को उलट करने पर कोई फर्क नहीं पड़ता था जब मैं इसका परीक्षण कर रहा था। वाईएमएमवी लेकिन मैंने हाल ही में इसका इस्तेमाल किया और यह दिए गए दिनों के लिए डुप्लिकेट लौटा। मेरी अंतिम क्वेरी 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; 

    } 

} 
संबंधित मुद्दे