使用主题和日期范围过滤Outlook电子邮件



我正在尝试从Outlook收件箱中过滤主题为"每日用水量"今天就寄出了

如果我只按主题过滤,代码是工作的。

当我按主题和日期过滤时,我没有得到任何结果。我没有得到任何错误。

Public Sub Download_wat()

Dim outlookApp As Outlook.Application 
Dim outlookInbox As Outlook.MAPIFolder 
Dim outlookRestrictItems As Outlook.Items 
Dim outlookLatestItem As Outlook.MailItem 
Dim outlookAttachment As Outlook.Attachment 
Dim subjectFilter As String
Dim flt As String 
Dim startDate As String 
Dim endDate As String
Const senderName As String = "Mech" 
Dim attachmentName As String  

subjectFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%Daily Consumption%'"      

'Create an instance of Outlook
Set outlookApp = New Outlook.Application

'Get the inbox from Outlook
Set outlookInbox = outlookApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

startDate = CStr(Date) & " " & "00:00"  'Date can be replaced with any string Date
endDate = CStr(Date + 1) & " " & "00:00"  'the same, it should be the previous Date +1
subjectFilter = "@SQL=urn:schemas:httpmail:subject" & "" & " ci_phrasematch 'Daily Water Consumption'"
flt = "[Subject] = 'subjectFilter' and [ReceivedTime] >= '" & startDate & "' and  [ReceivedTime] < '" & endDate & "'"

Set outlookRestrictItems = outlookInbox.Items.Restrict(flt)

'Check whether any items were found
If outlookRestrictItems.Count = 0 Then
MsgBox "No items were found from " & senderName & "!", vbExclamation
Exit Sub
End If

'Sort the filtered items by received time and in descending order
outlookRestrictItems.Sort Property:="[ReceivedTime]", Descending:=True

'Get the latest item from the filtered and sorted items

Set outlookLatestItem = outlookRestrictItems(1)
Debug.Print outlookLatestItem.Subject
End Sub

您可以过滤已过滤的主题项,而不是将它们连接到一个过滤器中。

Option Explicit
Public Sub Download_wat()

Dim outlookInbox As Folder
Dim outlookRestrictItemsSubj As Items
Dim outlookRestrictItems As Items

Dim outlookLatestItem As Object

' May not be a mailitem.
'Dim outlookLatestItem As outlook.MailItem

Dim subjectFilter As String
Dim timeFilter As String

Dim startDate As String
Dim endDate As String

'Get the inbox from Outlook
Set outlookInbox = Session.GetDefaultFolder(olFolderInbox)

startDate = CStr(Date) & " " & "00:00"  'Date can be replaced with any string Date
Debug.Print " startDate: " & startDate

endDate = CStr(Date + 1) & " " & "00:00"  'the same, it should be the previous Date +1
Debug.Print " endDate..: " & endDate

'subjectFilter = "@SQL=urn:schemas:httpmail:subject" & "" & " ci_phrasematch 'Daily Water Consumption'"
subjectFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%Daily Water Consumption%'"
Debug.Print " subjectFilter.: " & subjectFilter

Set outlookRestrictItemsSubj = outlookInbox.Items.Restrict(subjectFilter)
Debug.Print outlookRestrictItemsSubj.count

timeFilter = "[ReceivedTime] >= '" & startDate & "' and [ReceivedTime] < '" & endDate & "'"
Debug.Print " timeFilter: " & timeFilter

Set outlookRestrictItems = outlookRestrictItemsSubj.Restrict(timeFilter)
Debug.Print outlookRestrictItems.count

'Check whether any items were found
If outlookRestrictItems.count = 0 Then
MsgBox "No items were found!", vbExclamation
Exit Sub
End If

'Sort the filtered items by received time in descending order
outlookRestrictItems.Sort Property:="[ReceivedTime]", Descending:=True

'Get the latest item from the filtered and sorted items
Set outlookLatestItem = outlookRestrictItems(1)
Debug.Print outlookLatestItem.subject
Debug.Print outlookLatestItem.ReceivedTime

End Sub

最新更新