Filter Outlook Items on new profile load from current day to 15 days previous RRS feed

  • Question

  • Hi, 

    I have a Outlook addin in C# that performs a function on MeetingItems the user receives in their inbox. That part of the addin works great.

    Because this addin will be deployed to multiple users, there may be a case where the user needs to create a new Outlook profile, which would require Outlook to load possibly years worth of emails. The code in the add-in fires an event handler everytime an item is received and determines if its a MeetingItem type. If it is the code continues to process it. 
    The problem I am running into is if the user loads a new profile in Outlook, it checks if every single message that has been in their inbox. The filter I have tried to use I want to only "check" for MailItems in their inbox all days from today to 15 days previous, and then Outlook would load the profile as it normally would. When their profile is done loading and they begin to use as normal I want the  ItemAddEventHandler to fire whenever a new item appears in the inbox.


            Outlook.MAPIFolder inbox;
            Outlook.Items _items;

            private void ThisAddIn_Startup(object sender, System.EventArgs e)
                catch (Exception ex)

            private void ItemInRange()
                DateTime start = DateTime.Now;
                DateTime end = start.AddDays(-5);
                Outlook.Items rangeAppts = GetAppointmentsInRange(inbox, start, end);

                if (rangeAppts != null)
                    _items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(InboxFolderItemAdded);

            private Outlook.Items GetAppointmentsInRange(Outlook.MAPIFolder folder, DateTime startTime, DateTime endTime)
                string filter = "[End] >='" + endTime.ToString("g") + "' AND [Start] <= '" + endTime.ToString("g") + "'";
                    _items = folder.Items;

                    _items.IncludeRecurrences = true;
                    _items.Sort("[Start]", Type.Missing);
                    Outlook.Items restrictItems = _items.Restrict(filter);
                    if (_items.Count > 0)
                        return restrictItems;
                        return null;
                catch { return null; }

    However, when I run this _items.Count is zero and the filter function returns NULL. 

    However, if I dont use the filter and have the try in the startup event simply:



    inbox = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            _items = inbox.Items;
            _items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(InboxFolderItemAdded);  }then it will work, but be triggered for every item in the inbox of the profile being loaded.  Am I doing this filter correctly? Is there a way to apply the filter only to when/if the user is loading their profile for the first time? Thank you

    Monday, November 30, 2015 7:06 PM

All replies

  • Hello Darren,

    Make sure the time and date in the format Outlook can understand. Take a look at the following articles which illustrates the described functionality:

    How To: Retrieve Outlook calendar items using Find and FindNext methods

    How To: Use Restrict method in Outlook to get calendar items

    Also you may consider using the AdvancedSearch method of the Application method. The key benefits of using the AdvancedSearch method in Outlook are:

     - The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
     - Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
     - Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
     - Finally, you can stop the search process at any moment using the Stop method of the Search class.

    >  Is there a way to apply the filter only to when/if the user is loading their profile for the first time?

    You can add a windows registry key which can indicate whether the profile is loaded for the first time or not.

    p.s. a folder contains various types of items. So, I'd suggest checking the message class also.

    Monday, November 30, 2015 7:22 PM
  • Thank you Eugene.

    I am trying to use your examples but they do not work due to the fact that I am running the filter function in the ThisAddIn_Startup event/function, so if the profile is being loaded for the first time it does not see any items when Outlook is still on the boot up screen. Is there a way I can create an event handler to fire once Outlook has loaded (aka off the boot screen) that can then use the filter?

    Regarding the Windows Reg key, can you please elaborate?  if I run Outlook addin , Outlook asks if I want to load an existing mail profile or create a new one. If I create a new one in this Outlook boot/load profile screen, it gets added to the registry and before any of my addin code can run. When the addin code does run, it sees this new profile as existing in the registry.

    Monday, November 30, 2015 10:14 PM
  • You can use the Startup or MAPILogonComplete events of the Application class. The first one is fired when Microsoft Outlook is starting, but after all add-in programs have been loaded. And the last one is fired after the user has logged onto the system.

    You can create keys in the windows registry associated with the CurrentProfileName property value. So, each time the add-in is loaded by Outlook you may check the key which corresponds to the profile loaded and see whether the action was made or not (the key is absent, for example).

    Also you may consider using the StorageItem object which represents a message object in MAPI that is always saved as a hidden item in the parent folder and stores private data for Outlook solutions. A StorageItem object is stored at the folder level, allowing it to roam with the account and be available online or offline. For more information on storing solution data using the StorageItem object, see Storing Data for Solutions.

    Tuesday, December 1, 2015 10:29 AM