按日期限制 Outlook 项目 [英] Restrict Outlook Items by Date
问题描述
我有一个 Outlook 宏,可以按日期过滤电子邮件对象并根据数组返回项目.
I have an Outlook macro that filters email objects by date and returns items based on an array.
今天的过滤器如下:
sfilter = "[ReceivedTime]>=""&Date()12:00am&"""
Set myItems = myNewFolder.Items.Restrict(sfilter)
sFilter 是一个字符串,它按预期返回今天的项目.
sFilter is a string and this returns the items for today as intended.
我正在尝试过滤昨天收到的电子邮件.
I am trying to filter to emails received yesterday.
以下是我的尝试.
sfilter = "[ReceivedTime]>=""&Date(-1) 12:00am&"" AND [ReceivedTime]<= ""&Date() 12:00am&"" "
tfilter = Format(DateAdd("d", -1, Date), "mm/dd/yyyy")
rFilter = Format(DateAdd("d", 0, Date), "mm/dd/yyyy")
我打算使用 tFilter 和 rFilter 作为 sFilter 的上限和下限.
I intended to use the tFilter and rFilter as the upper and lower bound for sFilter.
我在 MSDN 站点上查看了函数信息后尝试使用 DateAdd 方法,但没有返回昨天的项目.
I tried to use the DateAdd method after looking on the MSDN site with the function information but that did not return yesterday's items.
我尝试了在这个问题上提供的解决方案(Outlook .Restrict 方法没有与日期一起工作).
I tried the solution offered on this question (Outlook .Restrict method does not work with Date).
带有 date(-1) 的方法不能与 date 一起使用.根据 MSDN 站点的逻辑运算符应该可以工作.
The method with date(-1) did not work in tandem with date. According to the MSDN site logical operators should work.
注意:下面三个例子引用编译并且不返回任何错误.
Note: The lower three examples cited compile and do not return any errors.
推荐答案
你可以用两个独立的 限制.
You can find yesterday's mail with two separate Restricts.
Private Sub EmailYesterday()
Dim oOlInb As Folder
Dim oOlItm As Object
Dim oOlResults As Object
Dim i As Long
Dim sFilter As String
Dim sFilter2 As String
Set oOlInb = Session.GetDefaultFolder(olFolderInbox)
'Filter recent - Lower Bound of the range
sFilter = "[ReceivedTime]>'" & format(Date - 1, "DDDDD HH:NN") & "'"
Debug.Print vbCr & sFilter
Set oOlResults = oOlInb.Items.Restrict(sFilter)
Debug.Print oOlResults.count & " items."
If oOlResults.count > 0 Then
For i = 1 To oOlResults.count
Set oOlItm = oOlResults(i)
Debug.Print oOlItm.Subject & " - " & oOlItm.ReceivedTime
Next i
End If
' Filter range - Upper Bound
sFilter2 = "[ReceivedTime]<'" & format(Date, "DDDDD HH:NN") & "'"
Debug.Print vbCr & sFilter; " AND " & sFilter2
Set oOlResults = oOlResults.Restrict(sFilter2) ' Restrict the Lower Bound result
Debug.Print oOlResults.count & " items."
If oOlResults.count > 0 Then
For i = 1 To oOlResults.count
Set oOlItm = oOlResults(i)
Debug.Print oOlItm.Subject & " - " & oOlItm.ReceivedTime
Next i
End If
ExitRoutine:
Set oOlInb = Nothing
Set oOlResults = Nothing
Debug.Print "Done."
End Sub
这篇关于按日期限制 Outlook 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!