Tuesday, July 9, 2013

Get Start Dates of a Recurring Outlook Appointment

Hi All,

I was able to create a C# stub in order to get the start dates of a recurring appointment in outlook. So far I have not found any errors. Feel free to modify the code if necessary.

private Collection GetOccurrences(DateTime startDate, RecurrencePattern recurrencePattern)
{
    Collection occurrences = new Collection();

    if (recurrencePattern != null && !recurrencePattern.NoEndDate)
    {
        switch (recurrencePattern.RecurrenceType)
        {
            case OlRecurrenceType.olRecursDaily:
                for (int i = 0; i < recurrencePattern.Occurrences; i++)
                {
                    occurrences.Add(startDate);
                    startDate = startDate.AddDays(1 * recurrencePattern.Interval);
                }

                break;
            case OlRecurrenceType.olRecursWeekly:
                if (recurrencePattern.Interval == 0 && (int)recurrencePattern.DayOfWeekMask == 62)
                {
                    int countW = 0;

                    while (countW < recurrencePattern.Occurrences)
                    {
                        if (startDate.DayOfWeek != DayOfWeek.Sunday && startDate.DayOfWeek != DayOfWeek.Saturday)
                        {
                            occurrences.Add(startDate);
                            countW++;
                        }

                        startDate = startDate.AddDays(1);
                    }
                }
                else if (recurrencePattern.Interval > 0)
                {
                    int countW = 0;

                    while (countW < recurrencePattern.Occurrences)
                    {
                        if (recurrencePattern.Interval == 1)
                        {
                            if (this.IsMaskedDay((int)recurrencePattern.DayOfWeekMask, startDate))
                            {
                                occurrences.Add(startDate);
                                countW++;
                            }

                            startDate = startDate.AddDays(1);
                        }
                        else
                        {
                            if (occurrences.Contains(startDate.AddDays(-7)))
                            {
                                startDate = startDate.AddDays(7 * (recurrencePattern.Interval - 1));
                            }

                            if (this.IsMaskedDay((int)recurrencePattern.DayOfWeekMask, startDate))
                            {
                                occurrences.Add(startDate);
                                countW++;
                            }

                            startDate = startDate.AddDays(1);
                        }
                    }
                }

                break;
            case OlRecurrenceType.olRecursMonthly:
                if (recurrencePattern.Instance == 0)
                {
                    for (int i = 0; i < recurrencePattern.Occurrences; i++)
                    {
                        occurrences.Add(startDate);
                        startDate = startDate.AddMonths(recurrencePattern.Interval);
                    }
                }

                break;
            case OlRecurrenceType.olRecursMonthNth:
                occurrences.Add(startDate);
                DateTime dateToAdd = startDate;
                int countMNth = 1;

                while (countMNth < recurrencePattern.Occurrences)
                {
                    dateToAdd = new DateTime(dateToAdd.Year, dateToAdd.AddMonths(recurrencePattern.Interval).Month, 1, dateToAdd.Hour, dateToAdd.Minute, dateToAdd.Second);
                    for (int i = 0; i < 7; i++)
                    {
                        if (this.IsMaskedDay((int)recurrencePattern.DayOfWeekMask, dateToAdd))
                        {
                            int currentMonth = dateToAdd.Month;
                            if (dateToAdd.AddDays(7 * (recurrencePattern.Instance - 1)).Month != currentMonth)
                            {
                                dateToAdd = dateToAdd.AddDays(7 * (recurrencePattern.Instance - 2));
                            }
                            else
                            {
                                dateToAdd = dateToAdd.AddDays(7 * (recurrencePattern.Instance - 1));
                            }

                            occurrences.Add(dateToAdd);
                            countMNth++;
                            break;
                        }
                        else
                        {
                            dateToAdd = dateToAdd.AddDays(1);
                        }
                    }
                }

                break;
            case OlRecurrenceType.olRecursYearly:
                if (recurrencePattern.Instance == 0)
                {
                    int countY = 0;

                    // TODO: Please use a for loop, since the number of iterations is known.
                    while (countY < recurrencePattern.Occurrences)
                    {
                        occurrences.Add(startDate);
                        countY++;
                        //startDate = startDate.AddYears(1);
                        //// Exception popping. please see
                        startDate = startDate.AddYears(recurrencePattern.Interval / 12);
                    }
                }

                break;
            case OlRecurrenceType.olRecursYearNth:
                occurrences.Add(startDate);
                DateTime dateToAddY = startDate;
                int countYNth = 1;

                while (countYNth < recurrencePattern.Occurrences)
                {
                    dateToAddY = new DateTime(dateToAddY.AddYears(recurrencePattern.Interval / 12).Year, dateToAddY.Month, 1, dateToAddY.Hour, dateToAddY.Minute, dateToAddY.Second);
                    for (int i = 0; i < 7; i++)
                    {
                        if (this.IsMaskedDay((int)recurrencePattern.DayOfWeekMask, dateToAddY))
                        {
                            int currentMonth = dateToAddY.Month;
                            if (dateToAddY.AddDays(7 * (recurrencePattern.Instance - 1)).Month != currentMonth)
                            {
                                dateToAddY = dateToAddY.AddDays(7 * (recurrencePattern.Instance - 2));
                            }
                            else
                            {
                                dateToAddY = dateToAddY.AddDays(7 * (recurrencePattern.Instance - 1));
                            }

                            occurrences.Add(dateToAddY);
                            countYNth++;
                            break;
                        }
                        else
                        {
                            dateToAddY = dateToAddY.AddDays(1);
                        }
                    }
                }

                break;
            default:
                break;
        }
    }

    return occurrences;
}

private bool IsMaskedDay(int maskedInt, DateTime date)
{
    string binaryString = Convert.ToString(maskedInt, 2);
    binaryString = new string('0', 7 - binaryString.Length) + binaryString;

    char[] binaryArray = binaryString.ToCharArray();
    Array.Reverse(binaryArray);

    return binaryArray[(int)date.DayOfWeek] == '1';
}

2 comments:

  1. Thanks for this! I think your IsMaskedDay could be better written as:

    private static bool IsMaskedDay(Outlook.OlDaysOfWeek dayOfWeek, DateTime date) {
    int startDay = (1 << (int)start.DayOfWeek);
    return (startDay & (int)dayOfWeek) == startDay;
    }

    ReplyDelete