public static void ListTaskCalendar() { var outlookApp = new Microsoft.Office.Interop.Outlook.Application(); var mapiNamespace = outlookApp.GetNamespace("MAPI"); var CalendarFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar); var 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()); } } }
/// <summary> /// Removes a specific event or a series of events /// </summary> /// <param name="objItems">Contains all the event items in a specific calendar/folder</param> /// <param name="subject">Subject of the event to delete</param> /// <param name="eventDate">If specified, this specific event is deleted instead of all events with subject</param> internal static void RemoveEvent(Outlook.Items objItems, string subject, string eventDate) { string methodTag = "RemoveEvent"; LogWriter.WriteInfo(TAG, methodTag, "Starting: " + methodTag + " in " + TAG); Outlook.AppointmentItem agendaMeeting = null; if (eventDate == null) { objItems.Sort("[Subject]"); objItems.IncludeRecurrences = true; LogWriter.WriteInfo(TAG, methodTag, "Attempting to find event with subject: " + subject); agendaMeeting = objItems.Find("[Subject]=" + subject); if (agendaMeeting == null) { LogWriter.WriteWarning(TAG, methodTag, "No event found with subject: " + subject); } else { LogWriter.WriteInfo(TAG, methodTag, "Removing all events with subject: " + subject); do { agendaMeeting.Delete(); agendaMeeting = objItems.FindNext(); LogWriter.WriteInfo(TAG, methodTag, "Event found and deleted, finding next"); } while (agendaMeeting != null); LogWriter.WriteInfo(TAG, methodTag, "All events with subject: " + subject + " found and deleted"); } } else { LogWriter.WriteInfo(TAG, methodTag, "Finding event with subject" + subject + " and date: " + eventDate); agendaMeeting = objItems.Find("[Subject]=" + subject); if (agendaMeeting == null) { LogWriter.WriteWarning(TAG, methodTag, "No event found with subject: " + subject); } else { Outlook.RecurrencePattern recurrPatt = agendaMeeting.GetRecurrencePattern(); agendaMeeting = recurrPatt.GetOccurrence(DateTime.Parse(eventDate)); agendaMeeting.Delete(); LogWriter.WriteInfo(TAG, methodTag, "Event found and deleted"); } } }
/// <summary> /// Sets recurrence exceptions in direction Google -> Outlook /// </summary> /// <param name="googleItem">Source Google event</param> /// <param name="outlookItem">Target Outlook event</param> private void SetRecurrenceExceptions(Event googleItem, object outlookItem) { //if (!this._googleExceptions.ContainsKey(googleItem.Id)) // return; ((Outlook.AppointmentItem)outlookItem).Save(); Outlook.RecurrencePattern outlookRecurrence = ((Outlook.AppointmentItem)outlookItem).GetRecurrencePattern(); try { if (this._googleExceptions.ContainsKey(googleItem.Id)) { foreach (Event googleException in this._googleExceptions[googleItem.Id]) { /// If the exception is already in Outlook event this one is omited //if (RecurrenceExceptionComparer.Contains(outlookRecurrence.Exceptions, googleException)) // continue; /// Get occurence of the recurrence and modify it. Thus new exception is created Outlook.AppointmentItem outlookExceptionItem = outlookRecurrence.GetOccurrence(googleException.OriginalStartTime.DateTime.Value); try { if (googleException.Status == "cancelled") { outlookExceptionItem.Delete(); } else { this.SetSubject(googleException, outlookExceptionItem, Target.Outlook); this.SetDescription(googleException, outlookExceptionItem, Target.Outlook); this.SetLocation(googleException, outlookExceptionItem, Target.Outlook); this.SetTime(googleException, outlookExceptionItem, Target.Outlook); outlookExceptionItem.Save(); } } finally { Marshal.ReleaseComObject(outlookExceptionItem); } } } } finally { Marshal.ReleaseComObject(outlookRecurrence); } }
// <Snippet1> private void ThisAddIn_Startup(object sender, System.EventArgs e) { Outlook.MAPIFolder calendar = Application.Session.GetDefaultFolder( Outlook.OlDefaultFolders.olFolderCalendar); Outlook.Items calendarItems = calendar.Items; Outlook.AppointmentItem item = calendarItems["Test Appointment"] as Outlook.AppointmentItem; Outlook.RecurrencePattern pattern = item.GetRecurrencePattern(); Outlook.AppointmentItem itemDelete = pattern. GetOccurrence(new DateTime(2006, 6, 28, 8, 0, 0)); if (itemDelete != null) { itemDelete.Delete(); } }
public bool CalendarItemConvertRecurrences() { try { bool ConvertedAny = false; #region var DateTime checkLast_At = DateTime.Parse(sqlController.SettingRead(Settings.checkLast_At)); double checkPreSend_Hours = double.Parse(sqlController.SettingRead(Settings.checkPreSend_Hours)); double checkRetrace_Hours = double.Parse(sqlController.SettingRead(Settings.checkRetrace_Hours)); int checkEvery_Mins = int.Parse(sqlController.SettingRead(Settings.checkEvery_Mins)); bool includeBlankLocations = bool.Parse(sqlController.SettingRead(Settings.includeBlankLocations)); DateTime timeOfRun = DateTime.Now; DateTime tLimitTo = timeOfRun.AddHours(+checkPreSend_Hours); DateTime tLimitFrom = checkLast_At.AddHours(-checkRetrace_Hours); #endregion #region convert recurrences foreach (Outlook.AppointmentItem item in GetCalendarItems(tLimitTo, tLimitFrom)) { if (item.IsRecurring) //is recurring, otherwise ignore { #region location "planned"? string location = item.Location; if (location == null) { if (includeBlankLocations) { location = "planned"; } else { location = ""; } } location = location.ToLower(); #endregion if (location == "planned") #region ... { Outlook.RecurrencePattern rp = item.GetRecurrencePattern(); Outlook.AppointmentItem recur = null; DateTime startPoint = item.Start; while (startPoint.AddYears(1) <= tLimitFrom) { startPoint = startPoint.AddYears(1); } while (startPoint.AddMonths(1) <= tLimitFrom) { startPoint = startPoint.AddMonths(1); } while (startPoint.AddDays(1) <= tLimitFrom) { startPoint = startPoint.AddDays(1); } for (DateTime testPoint = startPoint; testPoint <= tLimitTo; testPoint = testPoint.AddMinutes(checkEvery_Mins)) //KEY POINT { if (testPoint >= tLimitFrom) { try { recur = rp.GetOccurrence(testPoint); try { Appointment appo_Dto = new Appointment(recur.GlobalAppointmentID, recur.Start, item.Duration, recur.Subject, recur.Location, recur.Body, t.Bool(sqlController.SettingRead(Settings.colorsRule)), false, sqlController.Lookup); appo_Dto = CreateAppointment(appo_Dto); recur.Delete(); log.LogStandard("Not Specified", recur.GlobalAppointmentID + " / " + recur.Start + " converted to non-recurence appointment"); } catch (Exception ex) { log.LogWarning("Not Specified", t.PrintException(t.GetMethodName() + " failed. The OutlookController will keep the Expection contained", ex)); } ConvertedAny = true; } catch { } } } } #endregion } } #endregion if (ConvertedAny) { log.LogStandard("Not Specified", t.GetMethodName() + " completed + converted appointment(s)"); } else { log.LogEverything("Not Specified", t.GetMethodName() + " completed"); } return(ConvertedAny); } catch (Exception ex) { throw new Exception(t.GetMethodName() + " failed", ex); } }
/// <summary> /// Updates the attributes of a specific event /// </summary> /// <param name="calEvent">Event to be updated</param> /// <param name="eventDate">Date of the specific event to be updated M/d/yyyy hh:mm:ss tt</param> /// <param name="updatedTitle">Updated title if desired</param> /// <param name="updatedStartDate">Updated start date if desired</param> /// <param name="updatedDuration">Updated duration if desired</param> /// <param name="recipients">Additonal recipients if desired</param> /// <returns></returns> internal static Outlook.AppointmentItem UpdateEvent(Outlook.AppointmentItem calEvent, string eventDate, string updatedTitle, string updatedStartDate, string updatedDuration, string[] recipients) { string methodTag = "UpdateEvent"; LogWriter.WriteInfo(TAG, methodTag, "Starting: " + methodTag + " in " + TAG); Outlook.AppointmentItem agendaMeeting = null; Outlook.RecurrencePattern recurrPatt = null; if (calEvent != null) { if (eventDate != null) { LogWriter.WriteInfo(TAG, methodTag, "Obtaining specific event on date: " + eventDate + " with title: " + calEvent.Subject); recurrPatt = calEvent.GetRecurrencePattern(); agendaMeeting = recurrPatt.GetOccurrence(DateTime.Parse(eventDate)); } else { LogWriter.WriteWarning(TAG, methodTag, "No event date specified, may cause issues finding event if it is a recurring event"); agendaMeeting = calEvent; } if (updatedTitle != null) { LogWriter.WriteInfo(TAG, methodTag, "Updating the events title to: " + updatedTitle); agendaMeeting.Subject = updatedTitle; } else { LogWriter.WriteInfo(TAG, methodTag, "No updated subject specified, keeping subject the same"); } if (updatedDuration != null) { LogWriter.WriteInfo(TAG, methodTag, "Updating the events duration to: " + updatedDuration); agendaMeeting.Duration = Convert.ToInt32(updatedDuration); } else { LogWriter.WriteInfo(TAG, methodTag, "Updated duration not specified, keepign duration the same"); } foreach (String contact in recipients) { agendaMeeting.Recipients.Add(contact); LogWriter.WriteInfo(TAG, methodTag, "Adding recipient: " + contact + " to meeting invite"); } if (updatedStartDate != null) { LogWriter.WriteInfo(TAG, methodTag, "Updating the events start date to: " + updatedStartDate); agendaMeeting.Start = DateTime.Parse(updatedStartDate); } else { LogWriter.WriteInfo(TAG, methodTag, "Updated start date not specified, keeping start date the same"); } agendaMeeting.Save(); LogWriter.WriteInfo(TAG, methodTag, "Event updated sucessfully"); } else { LogWriter.WriteWarning(TAG, methodTag, "Calendar event is null, unable to continue with update event"); throw new NullReferenceException(); } return(agendaMeeting); }
private List <AppointmentInfo> LoadAllData() { List <AppointmentInfo> allData = new List <AppointmentInfo>(); Log.Write($"allData cleared."); try { foreach (var folder in AllMAPIFolders) { Log.Write($"LoadAllData: {folder.Folder.FullFolderPath}"); try { SetStatus(folder.ToString(), 0); Items items = folder.Folder.Items; if (items == null) { Log.Write($"LoadAllData: Items in folder {folder.Folder.FullFolderPath} is null!"); } items.IncludeRecurrences = true; Log.Write($"LoadAllData: Total {items.Count} items"); int n = 0; int lastProgress = 0; int p = 0; foreach (var item in items) { n += 1; p = 100 * n / items.Count; if (p != lastProgress) { SetStatus(null, p); lastProgress = p; } AppointmentItem ai = item as AppointmentItem; if (ai == null) { continue; } //Log.Write($"LoadAllData: Processing item {n} - {ai.Subject}"); if (ai.Sensitivity != 0) { // Log.Write("Skipping private event"); continue; } if (ai.IsRecurring) { // Log.Write($"LoadAllData: {ai.Subject} is recurring..."); Microsoft.Office.Interop.Outlook.RecurrencePattern rp = ai.GetRecurrencePattern(); Microsoft.Office.Interop.Outlook.AppointmentItem recur = null; for (DateTime cur = monthCalendar1.SelectionStart.AddHours(ai.Start.Hour).AddMinutes(ai.Start.Minute); cur < monthCalendar2.SelectionStart.AddDays(1); cur = cur.AddDays(1)) { try { recur = rp.GetOccurrence(cur); //Log.Write($"LoadAllData: Adding occurence of recurring item {ai.Subject} for date {cur}"); allData.Add(new AppointmentInfo(recur, folder)); } catch { } } } else { if ((ai.Start >= monthCalendar1.SelectionStart) && (ai.End < monthCalendar2.SelectionStart.AddDays(1))) { //Log.Write($"LoadAllData: Adding item {ai.Subject}"); allData.Add(new AppointmentInfo(ai, folder)); } else { //Log.Write($"LoadAllData: {ai.Subject} is out of date frame, continuing..."); continue; } } } } catch (System.Exception ex) { Log.Write($"[EXCEPTION] Nastala výjimka při zpracování složky {folder.Folder.Name}:\r\n {ex.Message}"); MessageBox.Show($"Nastala výjimka při zpracování složky {folder.Folder.Name}:\r\n {ex.Message}", "Výjimka", MessageBoxButtons.OK); } finally { folder.WasProcessed = true; } } } finally { SetStatus("Načteno", -1); } return(allData); }