How to filter Outlook mail items received by today

2019-09-17 17:23发布

I am new to Excel macros (VBA). We have an Excel macro application in which I am trying to filter outlook mail items received by today from other mail items. I have tried Restrict method. Here is the code looks now

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

But it throws error on execution. Any comment on this is highly appreciated.

3条回答
beautiful°
2楼-- · 2019-09-17 17:55

Firstly, your search criteria includes the function inside the string.

Secondly, you should never use "=" with date/time properties: the condition will never be satisfied due to round-off errors, always use a range.

In your case

olMiArr=Fldr1.Items.Restrict("[ReceivedTime] >= '" & DateValue(Now) & "' AND [ReceivedTime] < '" & DateValue(Now+1) & "'")
查看更多
一夜七次
3楼-- · 2019-09-17 17:56

There is another solution. When searching for Outlook items (be them mail items, task items ), use the DASL syntax. It looks more complicated, actually it is not.

There is an Outlook add-in called Outlook Spy (http://www.dimastr.com/outspy/home.htm) which provides you DASL strings. You can select the item whose field you wish to search upon and get the "DASL" formula for any field inside the item (regular fields or custom made fields).

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
查看更多
虎瘦雄心在
4楼-- · 2019-09-17 18:07

To make sure that the date is formatted as Microsoft Outlook expects, use the Format function. For example:

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

Also pay attention to the fact that the Restrict method applies a filter to the Items collection, returning a new collection containing all of the items from the original that match the filter.

查看更多
登录 后发表回答