Esempio n. 1
0
        private IEnumerable <Outlook.AppointmentItem> SplitAppointments(Outlook.AppointmentItem appointment, DateTime maxEnd)
        {
            Outlook.AppointmentItem splitAppointment;
            DateTime index = appointment.Start;
            DateTime eodOrAppointment;

            if (appointment.End < maxEnd)
            {
                maxEnd = appointment.End;
            }
            while (index < maxEnd)
            {
                // Set eod Or Appointment to the eod of the start, or if the date of the end and the date of the index are equal, set to end of appointment
                eodOrAppointment = maxEnd.Date == index.Date ? maxEnd : index.AddDays(1).Date;

                splitAppointment          = new NonOutlookAppointmentItem(appointment);
                splitAppointment.Duration = (int)(eodOrAppointment - index).TotalMinutes;
                splitAppointment.Start    = index;
                splitAppointment.End      = index.AddMinutes(splitAppointment.Duration);
                index = eodOrAppointment;
                yield return(splitAppointment);
            }
        }
Esempio n. 2
0
        private List <Outlook.AppointmentItem> GetOutlookAppointments(DateTime start, DateTime end)
        {
            var outlook    = new Microsoft.Office.Interop.Outlook.Application();
            var app        = outlook.Application;
            var appSession = app.Session;
            var calendar   = appSession.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
            //string filter = String.Format("[Start] >= {0} And [Start] < {1} And [End] > {0} And [End] <= {1}", start.ToString("ddddd h:nn AMPM"), end.ToString("ddddd h:nn AMPM"));
            string filter = string.Format("([Start] >= '{0}' AND [Start] < '{1}') OR ([End] > '{0}' AND [End] <= '{1}')", start.ToString("g"), end.ToString("g"));
            var    items  = calendar.Items;

            items.Sort("[Start]", Type.Missing);
            items.IncludeRecurrences = true;
            var restrictedItems = items.Restrict(filter);

            Outlook.AppointmentItem appointment;
            var appointments = new List <Outlook.AppointmentItem>();

            if (items.Count <= 0)
            {
                return(appointments);
            }
            foreach (var item in restrictedItems)
            {
                appointment = item as Outlook.AppointmentItem;
                if (appointment != null && !appointment.AllDayEvent && (appointment.BusyStatus != Outlook.OlBusyStatus.olOutOfOffice || Tasks.Any(t => string.Equals(t.Name, appointment.Subject, StringComparison.InvariantCultureIgnoreCase))))
                {
                    //if (appointment.IsRecurring)
                    //{
                    //    AddReoccuringAppointments(appointments, start, end, appointment);
                    //}
                    //else
                    if (appointment.Start.Date != appointment.End.Date)
                    {
                        appointments.AddRange(SplitAppointments(appointment, dtpEnd.Value.AddDays(1).Date));
                    }
                    else if (appointment.Start >= start && appointment.End <= end)
                    {
                        appointments.Add(appointment);
                    }
                }
            }

            //foreach (Outlook.AppointmentItem item in appointments)
            //{
            //    if (item.Conflicts.Count > 0)
            //    {
            //        Console.WriteLine("Never gets hit");
            //    }

            //}

            DateTime appointmentEnd = DateTime.MinValue;

            foreach (var appt in appointments.OrderBy(a => a.Start).ThenBy(a => a.CreationTime).ToList())
            {
                if (appointmentEnd > appt.Start)
                {
                    // remove appointment.  We may also need to edit it, but don't want to update Outlook with the changes...
                    appointments.Remove(appt);
                    if (appointmentEnd < appt.End)
                    {
                        // Update Appointment to be outside of first.
                        Outlook.AppointmentItem copy = new NonOutlookAppointmentItem(appt);
                        copy.Duration  = (int)(appt.End - appointmentEnd).TotalMinutes;
                        copy.Start     = appointmentEnd;
                        copy.End       = copy.Start.AddMinutes(copy.Duration);
                        appointmentEnd = copy.End;
                        appointments.Add(copy);
                    }
                }
                else
                {
                    appointmentEnd = appt.End;
                }
            }

            return(appointments.Where(a => a.Start != a.End).OrderBy(a => a.Start).ToList());
        }