如何从 Excel VBA 中的其他邮件项目中过滤今天收到的 Outlook 邮件项目

How to filter Outlook mail items received by today from other mail items in Excel VBA

我是 Excel 宏 (VBA) 的新手。 我们有一个 Excel 宏应用程序,我试图在其中从其他邮件项目中过滤今天收到的 outlook 邮件项目。我试过限制方法。 这是现在的代码

Set Fldr1 = olNs.GetDefaultFolder(olFolderInbox).Folders.Item("Folder name")
olMiArr=Fldr1.Items.Restrict("DateValue[ReceivedTime]='DateValue(Now)'")

但是它在执行时抛出错误。非常感谢对此发表任何评论。

要确保日期格式符合 Microsoft Outlook 的预期,请使用 Format 函数。例如:

Items.Restrict("DateValue[ReceivedTime]='" & Format(DateValue(Now),"ddddd h:nn AMPM") & "'")

另请注意,Restrict 方法对 Items 集合应用过滤器,返回一个新集合,其中包含原始集合中与过滤器匹配的所有项目。

首先,您的搜索条件包括 字符串中的函数。

其次,永远不要使用具有 date/time 属性的“=”:由于舍入误差,永远不会满足条件,始终使用范围。

你的情况

olMiArr=Fldr1.Items.Restrict("[ReceivedTime] >= '" & DateValue(Now) & "' AND [ReceivedTime] < '" & DateValue(Now+1) & "'")

还有一个解决办法。搜索 Outlook 项目(邮件项目、任务项目)时,请使用 DASL 语法。看起来比较复杂,其实不然。

有一个名为 Outlook Spy (http://www.dimastr.com/outspy/home.htm) 的 Outlook 加载项,它可以为您提供 DASL 字符串。您可以 select 您希望搜索其字段的项目,并获取项目内任何字段(常规字段或自定义字段)的 "DASL" 公式。

an example of usage below which reads a variable inside the search key (note the DASL string is random, it is just for the sake of description, you gotta generate it on your own Outlook item):

set the collection of filtered items like this. And loop inside it.

myFilter="@SQL=""http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/somefieldname/0x0000001F""='" & strEnterInput & "'"

Set objSourceItemsRestrict = objSourceItems.Restrict(myFilter)
For Each objSourceTask In objSourceItemsRestrict
'do something
Next