none
Find() not returning correct appointments RRS feed

  • Question

  • I am trying to get a list of appointments on a given day using the Find command.  I am using the following:

    List<string> times = new List<string>();
    
                Outlook.Folder folder = (Outlook.Folder)Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
    
                object obj = folder.Items.Find("[Start] > '" + start.ToString("M/d/yy h:mm tt") + "' AND [End] < '" + end.ToString("M/d/yy h:mm tt") + "'");
                Outlook.AppointmentItem appt;
    
                while (obj != null)
                {
                    appt = (Outlook.AppointmentItem)obj;
                    times.Add(appt.Subject + "\t" + appt.Start.Date.ToString() + "\t" + appt.End.Date.ToString());
                    obj = folder.Items.FindNext();
                }
    


    Start and End are 10/31/2011 7:00 AM and 10/31/2011 5:00 PM.

    I am only getting one result back from 10/17/2011.  I think I have the date formatted correctly... the VBA format string should be "ddddd h:nn AMPM" according to msdn.  What am I doing wrong?

    Monday, October 31, 2011 2:25 PM

All replies

  • Hi Justin,

    Thanks for your post.

    You should specify a items collection to call the FindNext method, check the modified code below:

           List<string> times = new List<string>();           
    
                Outlook.MAPIFolder folder = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
    
                Outlook.Items appointItems = folder.Items;
                string sFilter = "[Start] > '" + start.ToString("M/d/yy h:mm tt") + "' AND [End] < '" + end.ToString("M/d/yy h:mm tt") + "'";
               
                Outlook.AppointmentItem obj = appointItems.Find(sFilter);
       
    
                while (obj != null)
                {             
                    times.Add(obj.Subject + "\t" + obj.Start.Date.ToString() + "\t" + obj.End.Date.ToString());
                    //using appointItems to call  the FindNext method will resolve the issue.
                    obj = appointItems.FindNext();
                }
    

    For more information, please refer to :

    http://msdn.microsoft.com/en-us/library/ff862482.aspx


    I hope this helps


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, November 1, 2011 8:43 AM
    Moderator
  • Closer, but it still isn't returning the correct results.  I am getting things on my calendar from today as well as what looks to be every reoccurring meeting I have, regardless of whether or not there is an instance for today.
    Tuesday, November 1, 2011 12:56 PM
  • Hi Justin,

    While your code's logic seems intuitive, it actually won't work for the typical scenario. This is a common mistake that just about everyone makes, so don't feel bad. I think the best resource that explains how to properly set up the filter is here:

    http://blogs.msdn.com/b/waltwa/archive/2007/03/14/finding-appointments-within-a-specific-timeframe.aspx

    Please take a look at that and then let me know if you have any questions.

     


    Bill Jacob - Microsoft Customer Service & Support - Developer Messaging
    Thursday, November 3, 2011 5:03 PM
  • This is a very valid point, thanks for pointing it out.

    However, I am still getting what appears to be every recurring meeting that exists on my calendar regardless if it occurs in the timeframe or not.  I want the recurring meeting *if* it occurs in the timeframe.

    Thursday, November 3, 2011 5:50 PM
  • Can you please share your code as it stands now? Thanks!
    Bill Jacob - Microsoft Customer Service & Support - Developer Messaging
    Thursday, November 3, 2011 5:57 PM
  • private List<string> _getAvailability(DateTime start, DateTime end)
            {
                List<string> times = new List<string>();
    
                Outlook.Folder folder = (Outlook.Folder)Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
    
                Outlook.Items apptItems = folder.Items;
                apptItems.IncludeRecurrences = true;
                string filter = "[Start] <= '" + end.ToString("M/d/yy h:mm tt") + "' and [End] >= '" + start.ToString("M/d/yy h:mm tt") + "'";
                Outlook.AppointmentItem obj = apptItems.Find(filter);
    
                while (obj != null)
                {
                    times.Add(obj.Subject + "\t" + obj.Start.Date.ToString() + "\t" + obj.End.Date.ToString());
                    obj = apptItems.FindNext();
                }
    
    
                return times;
            }
    

    I tried with IncludeRecurrences = false as well... doesn't seem to drive anything I am using.
    • Edited by JustinRush Thursday, November 3, 2011 6:01 PM
    Thursday, November 3, 2011 6:00 PM
  • Hi Justin,

    It took me a while to notice it, but you need to add this line after you do the IncludeRecurrences:

    apptItems.Sort("[Start]");

    That should make it work.


    Bill Jacob - Microsoft Customer Service & Support - Developer Messaging

    Friday, November 4, 2011 3:18 PM
  • Same issue.  Here is the output of what I pull (each xx is a different meeting):

    this is a test
    start: 11/4/11 7:00 AM
    end: 11/4/11 5:00 PM
    meeting	start			end
    xx	11/14/2006 12:00:00 AM	11/14/2006 12:00:00 AM
    xx	5/18/2008 12:00:00 AM	5/19/2008 12:00:00 AM
    xx	11/6/2008 12:00:00 AM	11/6/2008 12:00:00 AM
    xx	12/6/2008 12:00:00 AM	12/7/2008 12:00:00 AM
    xx	1/10/2009 12:00:00 AM	1/10/2009 12:00:00 AM
    xx	1/13/2009 12:00:00 AM	1/13/2009 12:00:00 AM
    xx	5/15/2009 12:00:00 AM	5/16/2009 12:00:00 AM
    xx	7/1/2009 12:00:00 AM	7/2/2009 12:00:00 AM
    xx	10/19/2009 12:00:00 AM	10/19/2009 12:00:00 AM
    xx	11/20/2009 12:00:00 AM	11/20/2009 12:00:00 AM
    xx	2/17/2010 12:00:00 AM	2/17/2010 12:00:00 AM
    xx	4/29/2010 12:00:00 AM	4/29/2010 12:00:00 AM
    xx	8/31/2010 12:00:00 AM	8/31/2010 12:00:00 AM
    xx	10/13/2010 12:00:00 AM	10/13/2010 12:00:00 AM
    xx	2/8/2011 12:00:00 AM	2/8/2011 12:00:00 AM
    xx	2/14/2011 12:00:00 AM
    xx	5/10/2011 12:00:00 AM	5/10/2011 12:00:00 AM
    xx	6/28/2011 12:00:00 AM	6/28/2011 12:00:00 AM
    xx	7/7/2011 12:00:00 AM	7/7/2011 12:00:00 AM
    xx	8/31/2011 12:00:00 AM	9/1/2011 12:00:00 AM
    xx	10/17/2011 12:00:00 AM	10/17/2011 12:00:00 AM
    xx	10/17/2011 12:00:00 AM	10/17/2011 12:00:00 AM
    xx	11/4/2011 12:00:00 AM	11/4/2011 12:00:00 AM
    


    Friday, November 4, 2011 5:44 PM
  • Well, that seems odd, because it's working for me. Without the sort, it returns the All Day Events, and with the sort, it returns just those appointments on that day.

    Are these the only all-day events you have in this calendar? Or is the code just picking a subset?

    I'd sugget creating a subfolder with some clean test data and try running the code on that. There's a lot of things that can touch appointments and update properties on them, so this may be specific to your data. It doesn't sound likely, but I'd rule that out to be safe.


    Bill Jacob - Microsoft Customer Service & Support - Developer Messaging
    Friday, November 4, 2011 5:56 PM
  • There are only a couple of all day events in there... my output was only showing the date by mistake.  It may be including every single reoccurring meeting I have.
    Friday, November 4, 2011 6:02 PM
  • Here is he code I was testing (by just putting it into the Startup event of another add-in I had):

                DateTime start = new DateTime(2011, 10, 31, 7, 0, 0);
                DateTime end = new DateTime(2011, 10, 31, 17, 0, 0);
    
                Outlook.Folder folder = (Outlook.Folder)Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
    
                Outlook.Items apptItems = folder.Items;
                apptItems.IncludeRecurrences = true;
                apptItems.Sort("[Start]");
                string filter = "[Start] <= '" + end.ToString("M/d/yy h:mm tt") + "' and [End] >= '" + start.ToString("M/d/yy h:mm tt") + "'";
                //string filter = "[Start] <= '10/31/2011 5:00 PM' and [End] >= '10/31/2011 7:00 AM'";
                Debug.WriteLine(filter);
    
                // RESTRICT
                //Outlook.Items oItems = apptItems.Restrict(filter);
                //foreach (Outlook.AppointmentItem obj in oItems)
                //{
                //    Debug.WriteLine(obj.Subject + "\t" + obj.Start.Date.ToString() + "\t" + obj.End.Date.ToString());
                //}
    
    
                // FIND
                Outlook.AppointmentItem obj = apptItems.Find(filter);
                while (obj != null)
                {
                    Debug.WriteLine(obj.Subject + "\t" + obj.Start.Date.ToString() + "\t" + obj.End.Date.ToString());
                    obj = apptItems.FindNext();
                }

    Both Find and Restrict worked, as well as short and long years.

     


    Bill Jacob - Microsoft Customer Service & Support - Developer Messaging
    Friday, November 4, 2011 6:26 PM
  • Do you have reoccurring meetings on your calendar in the folder that is being searched?

     

    I am using Outlook 2010 / Exchange 2010 / VS 2010, .NET 4.0 .... if it matters

    • Edited by JustinRush Sunday, November 6, 2011 12:29 PM
    Sunday, November 6, 2011 12:28 PM
  • I'm using the same versions and have a variety of recurring appointments since it's my work calendar. I also added various recurring appointment types for that day, including on that falls within the time range, one that overlaps the time range, one that is an all-day event, etc.

    At this point I think you really need to narrow down whether this problem is specific to the data or the code. In addition to trying clean data, you could also try the VBA code sample in the previous link I referred you to. That may help indicate whether there's perhaps something else in your code coming into play.


    Bill Jacob - Microsoft Customer Service and Support - Messaging Developer
    Monday, November 7, 2011 4:24 PM