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.
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
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).
To make sure that the date is formatted as Microsoft Outlook expects, use the Format function. For example:
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.