How to filter mails using received time using vba

霸气de小男生 提交于 2019-12-13 20:05:01

问题


I am trying to find a way to filter email based on multiple criteria however i while running the below code i am getting an error "Cannot Parse Condition.Error at"09". The ReceivedDate is 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:


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

([ReceivedTime] > Date1) AND ([ReceivedTime] < Date2)



回答2:


There is indeed an error in the filter string. Where you used:

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 & "'"

The filter conditions for date and time must be passed as strings - and yours were missing the singlequotes.




回答3:


First of all, you need to format the date and time object so Outlook can understand it for comparing with actual values. For example, you can use the ToSting method of the DateTime structure:

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 +"\"";

You may find the following articles with the sample code in VB.NET included helpful:

  • How To: Retrieve Outlook calendar items using Find and FindNext methods
  • How To: Use Restrict method in Outlook to get calendar items

In case of VBA macros you may use the Format function. Here is what MSDN states:

Dates and times are typically stored with a Date format, the Find and Restrict methods require that the date and time be converted to a string representation. To make sure that the date is formatted as Microsoft Outlook expects, use the Format function. The following example creates a filter to find all contacts that have been modified after January 15, 1999 at 3:30 P.M.

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


来源:https://stackoverflow.com/questions/35084636/how-to-filter-mails-using-received-time-using-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!