How to filter emails using received time using vba

I am trying to find a way to filter email based on several criteria, but when I run the code below, I get the error "Unable to parse .Error condition on '09. ReceivedDate - 8/24/2008 9: 55:30 pm

ReceivedDate = Me.cballocation.Column(1)
Sender = Me.cballocation.Column(2)
Subject = Me.cballocation.Column(0)

sFilter = "[subject] = '" & Subject & "' and DateValue[ReceivedTime]=" & Format$(ReceivedDate, "ddddd h:nn AMPM") & " and " & "[Sender]= '" & Sender & "'"
Set Ns = ol.GetNamespace("MAPI")
Set ml = Ns.Folders("MIMUMBAI").Folders("Inbox").Folders("Completed")
Set ml = ml.Items.Restrict(sFilter)
+1
source share
3 answers

DateValue [ReceivedTime] is not a valid condition. You must use range

([ReceivedTime] > Date1) AND ([ReceivedTime] < Date2)
+1
source

There really is an error in the filter string. Where did you use:

sFilter = "[subject] = '" & Subject & "' and DateValue[ReceivedTime]=" & Format$(ReceivedDate, "ddddd h:nn AMPM") & " and " & "[Sender]= '" & Sender & "'"

You should have used:

sFilter = "[subject] = '" & Subject & "' and DateValue[ReceivedTime]='" & Format$(ReceivedDate, "ddddd h:nn AMPM") & "' and " & "[Sender]= '" & Sender & "'"

Filter conditions for the date and time must be passed as strings - and you did not have single quotes.

0
source

, , Outlook . , ToSting DateTime:

DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
                           DateTime.Now.Day, 23, 59, 00, 00);
string dateTimeEnd = dt.ToString("MM/dd/yyyy hh:mm tt");
string searchCriteria = "[Start]<=\"" + dateTimeEnd + "\"" + " AND [End]>=\""+ dateTimeStart +"\"";

VB.NET, :

VBA Format. MSDN:

Dates and times are usually stored in date format, the Find and Restrict methods require that the date and time be converted to a string representation. To ensure that the date is formatted as expected by Microsoft Outlook, use the Format function. The following example creates a filter to search for all contacts that were changed after January 15, 1999 at 3:30 pm.

  sFilter = "[LastModificationTime] > '" & Format("1/15/99 3:30pm", "ddddd h:nn AMPM") & "'"
0
source

Source: https://habr.com/ru/post/1692235/


All Articles