1

我只想使用 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()- 方法失败了?

4

1 回答 1

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
于 2013-06-12T19:38:51.610 回答