在新配置文件加载时将Outlook项目从当前日期过滤到之前的15天 [英] Filter Outlook Items on new profile load from current day to 15 days previous

查看:59
本文介绍了在新配置文件加载时将Outlook项目从当前日期过滤到之前的15天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

我在C#中有一个Outlook插件,用于在用户收件箱中收到的MeetingItems上执行一项功能。 addin的那部分工作得很好。

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.

因为这个插件将被部署到多个用户,所以可能存在用户需要创建新的Outlook配置文件的情况,这需要Outlook到加载可能需要多年的电子邮件。每次收到
项时,加载项中的代码都会触发事件处理程序,并确定它是否为MeetingItem类型。如果是代码继续处理它。 

我遇到的问题是,如果用户在Outlook中加载新的配置文件,它会检查是否每个邮件都在其收件箱中。我试图使用的过滤器我只想"检查"对于MailItems在其收件箱中的所有日期从现在开始到15天前的
,然后Outlook将像往常一样加载配置文件。当他们的个人资料完成加载并且他们开始正常使用时,我希望只要新项目出现在收件箱中就会触发ItemAddEventHandler。

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收件箱;

        Outlook.Items _items;



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

        {

           试试
            {

                ItemInRange();

            }¥b $ b            catch(例外情况)

        }


        private void ItemInRange()

        {

            DateTime start = DateTime.Now;

            DateTime end = start.AddDays(-5);

            Outlook.Items rangeAppts = GetAppointmentsInRange(收件箱,开始,结束);



            if(rangeAppts!= null)

            {

                _items.ItemAdd + = new Outlook.ItemsEvents_ItemAddEventHandler(InboxFolderItemAdded);

            }¥b $ b        }


        private Outlook.Items GetAppointmentsInRange(Outlook.MAPIFolder文件夹,DateTime startTime,DateTime endTime)

        {

  &NBSP; &NBSP; &NBSP; &NBSP;    string filter =" [End]> ='" + endTime.ToString(" g")+"'AND [Start]< ='" + endTime.ToString(" g")+"'" ;;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;试试
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _items = folder.Items;
$


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _items.IncludeRecurrences = true;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _items.Sort(" [[Start]",Type.Missing);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Outlook.Items restrictItems = _items.Restrict(filter);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(_items.Count> 0)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return restrictItems;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;否则

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;返回null;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; catch {return null; }¥b $ b  &NBSP; &NBSP; &NBSP; }

        Outlook.MAPIFolder inbox;
        Outlook.Items _items;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            try
            {
                ItemInRange();
            }
            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") + "'";
            try
            {
                _items = folder.Items;

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

然而,当我运行时,_items.Count为零且过滤器函数返回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) ;

  &NBSP; &NBSP; &NBSP; _items = inbox.Items;

  &NBSP; &NBSP; &NBSP; _items.ItemAdd + = new Outlook.ItemsEvents_ItemAddEventHandler(InboxFolderItemAdded);  }然后它会工作,但会被加载的配置文件的收件箱中的每个项目触发。  我正确地做了这个过滤器吗?
有没有办法仅在用户第一次加载他们的个人资料时应用过滤器?谢谢

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

推荐答案

Hello Darren,

Hello Darren,

确保Outlook格式的时间和日期可以理解。请看下面的文章,说明所描述的功能:

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

如何:使用Find和FindNext方法检索Outlook日历项

如何:使用限制方法获取日历项的Outlook

您也可以考虑使用
AdvancedSearch
方法。在Outlook中使用AdvancedSearch方法的主要好处是:

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

  - 搜索在另一个线程中执行。您不需要手动运行另一个线程,因为AdvancedSearch方法会在后台自动运行它。

  - 搜索任何项目类型的可能性:邮件,约会,日历,备注等任何位置,即超出某个文件夹的范围。 Restrict和Find / FindNext方法可以应用于特定的Items集合(请参阅Outlook中Folder类的Items属性
。)
  - 完全支持DASL查询(自定义)属性也可用于搜索)。您可以在MSDN中的过滤文章中阅读有关此内容的更多信息。为了提高搜索性能,如果为
商店启用即时搜索,则可以使用即时搜索关键字(请参阅Store类的IsInstantSearchEnabled属性)。

  - 最后,您可以使用Search类的Stop方法随时停止搜索过程。

 - 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?

您可以添加一个Windows注册表项,以指示是否首次加载配置文件。

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

p.s。文件夹包含各种类型的项目。所以,我建议也检查邮件类。

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


这篇关于在新配置文件加载时将Outlook项目从当前日期过滤到之前的15天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆