Outlook.Items Restrict()奇怪的行为

时间:2013-06-12 12:14:17

标签: c# outlook interop office-interop restrict

我只想用Restrict-Method过滤我的邮件,如下所示:

            restriction += "[ReceivedTime] < '" + ((DateTime)time).ToString("yyyy-MM-dd HH:mm") + "'";
            var count = oFolder.Items.Restrict(restriction).Count;//Cast<object>().ToList();
            for (int i = 0; i < count; i++)
            {
                var crntReceivedTime = ((OutLook.MailItem)oFolder.Items.Restrict(restriction).Cast<object>().ToList()[i]).ReceivedTime;
                if (crntReceivedTime > time)
                {
                    string t = "";
                }
            }

理论上,永远不应该调用行string t = "";,因为我确定Items永远不会有ReceivedTime值大于time的条目。 问题是该行被调用,这意味着受限制的Items Collection包含其不应包含的条目。

我做错了什么或者Restrict() - 方法失败了吗?

1 个答案:

答案 0 :(得分:2)

首先,您使用多点符号。你在循环的每一步调用Restrict(即使它被调用一次也很昂贵)。调用一次,缓存返回的(受限制的)Items集合,然后遍历该集合中的项目。

其次,完全限制是什么?您正在使用+ =在ReceivedTime上添加额外限制。限制变量的实际值是多少?

编辑:我从OutlookSpy执行以下脚本没有问题(单击脚本按钮,粘贴脚本,单击运行):

restriction = " [ReceivedTime] < '2011-06-11 00:00' "
set Folder = Application.ActiveExplorer.CurrentFolder
set restrItems = Folder.Items.Restrict(restriction)
for each item in restrItems
  if TypeName(item) = "MailItem" Then
    Debug.Print item.ReceivedTime & " - " & item.Subject
  End If
next