Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6906907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:22:10+00:00 2026-05-27T08:22:10+00:00

Background : I’m writing an application in C# (.NET 3.5), that looks at multiple

  • 0

Background: I’m writing an application in C# (.NET 3.5), that looks at multiple users Outlook 2003 calendars (using COM objects), gets the appointments and inserts the data for those appointments into a database.

Problem: After the first users calendar, any recurring items on the following calendars, will always have a start and end time of the first occurrence of that item. I’m releasing the COM Objects between users (and during, if the user has a lot of items), and The item collection is being restricted correctly (due to there only being a handful of the recurring tasks inserted (albeit the wrong start/end) instead of infinite from the “no end” tasks). The correct start/end time is part of the requirements, having the information available for this or another application to work out how much free time a user has for a given range of dates, and working hours.

Code: (Variable declarations omitted, they’re at the top of the relevant functions)

Looping through the users (in Main()):

 foreach (DataRow drUserCalendar in dtCalendars.Rows)
                {
                    //for each calendar we're looking at, export their calendar data and put it in the database
                    try
                    {
                        appOutlook = new Outlook.Application();
                        ExportCalendar(drUserCalendar);
                        Marshal.FinalReleaseComObject(appOutlook);
                        GC.Collect();
                    }
                    catch (Exception ex)
                    {
                        //report error
                    }
                }

Extracting information from the calendar

static void ExportCalendar(DataRow drUser)
        {                    
            strDisplayName = drUser["DisplayName"].ToString();
            strUserID = drUser["ID"].ToString();

            int.TryParse(drUser["PreviousDays"].ToString(), out intPrevious);
            int.TryParse(drUser["FutureDays"].ToString(), out intFuture);

            dtmAllowedPreviousStart = DateTime.Now.AddDays(-intPrevious);
            dtmAllowedFutureStart = DateTime.Now.AddDays(intFuture);

            nsOne = appOutlook.GetNamespace("MAPI");
            nsOne.Logon(null, null, false, false);
            rcpOne = nsOne.CreateRecipient(strDisplayName);

            intCount = 0;

            if (rcpOne.Resolve())
            {
                fldOne = nsOne.GetSharedDefaultFolder(rcpOne, Outlook.OlDefaultFolders.olFolderCalendar);

                strRestrict = "[Start] > '" + MIN_START_DATE.ToString("g") + "' And [End] < '" + MAX_START_DATE.ToString("g") + "'";
                itms = fldOne.Items;
                itms.Sort("[Start]", Type.Missing);
                itms.IncludeRecurrences = true;
                itmsRestricted = itms.Restrict(strRestrict);
                itmsRestricted.Sort("[Start]", Type.Missing);
                itmsRestricted.IncludeRecurrences = true;
                blnIsRecurring = false;
                dicRecurringTaskTracker = new Dictionary<string, int>();

                foreach (object objOne in itmsRestricted)
                {

                    if (intCount >= 100 || blnIsRecurring)
                    {
                        //release COM objects. Outlook doesn't like you having more than 250 ish items without cleaning up.
                        Marshal.FinalReleaseComObject(appOutlook);
                        appOutlook = new Outlook.Application();
                        GC.Collect();
                        intCount = 0;
                    }

                    if (objOne is Outlook.AppointmentItem)
                    {
                        appItem = (Outlook.AppointmentItem)objOne;
                        blnException = false;

                        //get data from the item
                        strEntryID = appItem.EntryID;
                        strSubject = appItem.Subject;
                        strBody = appItem.Body;
                        dtmStart = appItem.Start;
                        dtmEnd = appItem.End;

                        blnException = EXCEPTIONS.Contains(strSubject);

                        //if the item is an exception we're done with it.
                        if (!blnException)
                        {
                            strRecurrenceInterval = "";
                            strRecurrenceType = "";
                            strRecurrenceInfo = "";


                            //check if it's a recurring task.
                            blnIsRecurring = appItem.IsRecurring;
                            if (blnIsRecurring)
                            {
                                //check to see if we've already had a task from this series
                                if (!dicRecurringTaskTracker.Keys.Contains(strEntryID))
                                {
                                    //Start at 0 so the first (this) task
                                    //is number 1.
                                    dicRecurringTaskTracker.Add(strEntryID, 0);
                                }

                                //update number
                                dicRecurringTaskTracker[strEntryID] += 1;
                                //change the subject to add the count on the end
                                strEntryID = strEntryID + '-' + dicRecurringTaskTracker[strEntryID].ToString();

                                //it's a recurring task, so we need to find out when/how often.
                                rpTaskRecurrence = appItem.GetRecurrencePattern();
                                rtTaskRecurrenceType = rpTaskRecurrence.RecurrenceType;
                                strRecurrenceType = rtTaskRecurrenceType.ToString();
                                strRecurrenceInterval = rpTaskRecurrence.Interval.ToString();

                                switch (strRecurrenceType)
                                {
                                    case "olRecursDaily":
                                    case "olRecursMonthNth":
                                    case "olRecursWeekly":
                                        strRecurrenceInfo = rpTaskRecurrence.DayOfWeekMask.ToString();
                                        break;
                                    case "olRecursMonthly":
                                        strRecurrenceInfo = rpTaskRecurrence.DayOfMonth.ToString();
                                        break;
                                }
                            } 

                            if (strEntryID != null && strSubject != null && dtmStart != null && dtmEnd != null
                                && (intPrevious == 0 || (dtmStart > dtmAllowedPreviousStart)) && (intFuture == 0 || (dtmStart < dtmAllowedFutureStart)))
                            {
                                //build up the SQL
                                strSQL = "EXEC UpdateCalendarEntry ";
                                strSQL += "@EntryID='" + strEntryID + "', ";
                                strSQL += "@Subject='" + strSubject.Replace("'", "''") + "', ";
                                strSQL += "@Body='" + strSubject.Replace("'", "''") + "', ";
                                strSQL += "@StartDate='" + dtmStart.ToString("dd-MMM-yyyy HH:mm:ss") + "', ";
                                strSQL += "@EndDate='" + dtmEnd.ToString("dd-MMM-yyyy HH:mm:ss") + "', ";
                                strSQL += "@UserCalendarID=" + strUserID + ",";
                                strSQL += "@Recurring = " + blnIsRecurring.ToString() + ",";
                                strSQL += "@RecurrenceType = '" + strRecurrenceType + "',";
                                strSQL += "@RecurrenceInterval = '" + strRecurrenceInterval + "',";
                                strSQL += "@RecurrenceInfo = '" + strRecurrenceInfo + "';";

                                try
                                {
                                    //Execute SQL
                                }
                                catch (Exception ex)
                                {
                                    //Print error message
                                    MessageBox.Show(ex.ToString());
                                }
                            }
                        }
                        Marshal.FinalReleaseComObject(appItem);
                        GC.Collect();
                    }
                    strEntryID = null;
                    strSubject = null;
                    strBody = null;
                    intCount++;
                }

                //finished looping, do some clean up. 
                Marshal.FinalReleaseComObject(nsOne);
                Marshal.FinalReleaseComObject(rcpOne);
                Marshal.FinalReleaseComObject(fldOne);
                Marshal.FinalReleaseComObject(itms);
                Marshal.FinalReleaseComObject(itmsRestricted);
                GC.Collect();
            }
            else
            {
                throw new Exception("Could not resolve name");
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:22:11+00:00Added an answer on May 27, 2026 at 8:22 am

    After testing I’ve found that the issue relates to my (or whoever runs the application) permissions on the users (shared) calendar(s) that are being looked at.

    It works on the first user because that’s myself in this case. It doesn’t work on users after that because i don’t have sufficient rights it seems (confirmed by having my colleague changing it so default users are “owners” on his calendar and running the application again, and it working for his calendar).

    I have since tried to use GetOccurrence(DateTime) (surrounded with a while loop), however that lead to the same issue. The function would error when there was no occurrence (as expected), but when it found an occurrence it would return a null object.

    However because I wasn’t using the object for anything other than getting the start and end date of the task, I worked it out manually (I had the original task, meaning i could get the duration, by incrementing each day until I get an occurrence I’d get the reccurring tasks start date, and using the duration of the task I calculated the end date).

    It’s not an ideal solution, but if you’re just wanting to get recurring tasks it’s simple enough to do (though resource consuming if you have a lot of reccurring tasks and are looping for a long period of time)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: At my company we are developing a bunch applications that are using the
Background I am writing and using a very simple CGI-based (Perl) content management tool
Background: I have a website that has been built with ASP.NET 2.0 and is
Background: I am customizing an existing ASP .NET / C# application. It has it's
Background: I've wrote a small library that is able to create asp.net controls from
BACKGROUND I am automating an PowerPoint 2007 via C# I am writing unittests using
Background Given that 'most' developers are Business application developers, the features of our favorite
Background: I've inherited a web application that is intended to create on-the-fly connections between
Background: I'm in the process of creating a web service using ASP.NET 2.0. This
Background .NET 4, C#, MVC3, using JsonFx to serialize and deserialize data. Base controller

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.