英文:
Filtering Outlook email using subject and date range
问题
我正在尝试从Outlook收件箱中筛选主题为“每日水消耗”并且是今天发送的电子邮件。
如果只按主题筛选,代码是可以工作的。
当我同时按主题和日期筛选时,就没有结果了。我没有收到任何错误。
```vba
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 '%每日消耗%'"
'创建Outlook实例
Set outlookApp = New Outlook.Application
'从Outlook获取收件箱
Set outlookInbox = outlookApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
startDate = CStr(Date) & " " & "00:00" '日期可以替换为任何字符串日期
endDate = CStr(Date + 1) & " " & "00:00" '同样,它应该是前一天的日期+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)
'检查是否找到任何项目
If outlookRestrictItems.Count = 0 Then
MsgBox "未找到来自" & senderName & "的任何项目!", vbExclamation
Exit Sub
End If
'按接收时间对筛选的项目进行排序,降序排列
outlookRestrictItems.Sort Property:="[ReceivedTime]", Descending:=True
'从筛选和排序的项目中获取最新的项目
Set outlookLatestItem = outlookRestrictItems(1)
Debug.Print outlookLatestItem.Subject
End Sub
英文:
I am trying to filter an email from Outlook Inbox with subject " Daily Water Consumption" and sent today.
The code is working if I filter by subject only.
When I filter by subject and date both I am not getting any result. I am not getting any error.
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
答案1
得分: 0
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" '日期可以替换为任何字符串日期
Debug.Print " startDate: " & startDate
endDate = CStr(Date + 1) & " " & "00:00" '同样的,应该是前一天的日期+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
英文:
You can filter the filtered subject items rather than concatenating into one filter.
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论