使用主题和日期范围筛选 Outlook 电子邮件

huangapple go评论81阅读模式
英文:

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 = &quot;Mech&quot; 
    Dim attachmentName As String  
    
    subjectFilter = &quot;@SQL=&quot; &amp; Chr(34) &amp; &quot;urn:schemas:httpmail:subject&quot; &amp; Chr(34) &amp; &quot; like &#39;%Daily Consumption%&#39;&quot;      
    
    &#39;Create an instance of Outlook
    Set outlookApp = New Outlook.Application
    
    &#39;Get the inbox from Outlook
    Set outlookInbox = outlookApp.GetNamespace(&quot;MAPI&quot;).GetDefaultFolder(olFolderInbox)
    
    startDate = CStr(Date) &amp; &quot; &quot; &amp; &quot;00:00&quot;  &#39;Date can be replaced with any string Date
    endDate = CStr(Date + 1) &amp; &quot; &quot; &amp; &quot;00:00&quot;  &#39;the same, it should be the previous Date +1
    subjectFilter = &quot;@SQL=urn:schemas:httpmail:subject&quot; &amp; &quot;&quot; &amp; &quot; ci_phrasematch &#39;Daily Water Consumption&#39;&quot;
    flt = &quot;[Subject] = &#39;subjectFilter&#39; and [ReceivedTime] &gt;= &#39;&quot; &amp; startDate &amp; &quot;&#39; and  [ReceivedTime] &lt; &#39;&quot; &amp; endDate &amp; &quot;&#39;&quot;
    
    Set outlookRestrictItems = outlookInbox.Items.Restrict(flt)
    
    &#39;Check whether any items were found
    If outlookRestrictItems.Count = 0 Then
        MsgBox &quot;No items were found from &quot; &amp; senderName &amp; &quot;!&quot;, vbExclamation
        Exit Sub
    End If
    
    &#39;Sort the filtered items by received time and in descending order
    outlookRestrictItems.Sort Property:=&quot;[ReceivedTime]&quot;, Descending:=True
    
    &#39;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
    
    &#39; May not be a mailitem.
    &#39;Dim outlookLatestItem As outlook.MailItem
    
    Dim subjectFilter As String
    Dim timeFilter As String
    
    Dim startDate As String
    Dim endDate As String
    
    &#39;Get the inbox from Outlook
    Set outlookInbox = Session.GetDefaultFolder(olFolderInbox)
    
    startDate = CStr(Date) &amp; &quot; &quot; &amp; &quot;00:00&quot;  &#39;日期可以替换为任何字符串日期
    Debug.Print &quot; startDate: &quot; &amp; startDate
    
    endDate = CStr(Date + 1) &amp; &quot; &quot; &amp; &quot;00:00&quot;  &#39;同样的,应该是前一天的日期+1
    Debug.Print &quot; endDate..: &quot; &amp; endDate
    
    &#39;subjectFilter = &quot;@SQL=urn:schemas:httpmail:subject&quot; &amp; &quot;&quot; &amp; &quot; ci_phrasematch &#39;Daily Water Consumption&#39;&quot;
    subjectFilter = &quot;@SQL=&quot; &amp; Chr(34) &amp; &quot;urn:schemas:httpmail:subject&quot; &amp; Chr(34) &amp; &quot; like &#39;%Daily Water Consumption%&#39;&quot;
    Debug.Print &quot; subjectFilter.: &quot; &amp; subjectFilter
    
    Set outlookRestrictItemsSubj = outlookInbox.Items.Restrict(subjectFilter)
    Debug.Print outlookRestrictItemsSubj.count
    
    timeFilter = &quot;[ReceivedTime] &gt;= &#39;&quot; &amp; startDate &amp; &quot;&#39; and [ReceivedTime] &lt; &#39;&quot; &amp; endDate &amp; &quot;&#39;&quot;
    Debug.Print &quot; timeFilter: &quot; &amp; timeFilter
    
    Set outlookRestrictItems = outlookRestrictItemsSubj.Restrict(timeFilter)
    Debug.Print outlookRestrictItems.count
    
    &#39;Check whether any items were found
    If outlookRestrictItems.count = 0 Then
        MsgBox &quot;No items were found!&quot;, vbExclamation
        Exit Sub
    End If
    
    &#39;Sort the filtered items by received time in descending order
    outlookRestrictItems.Sort Property:=&quot;[ReceivedTime]&quot;, Descending:=True
    
    &#39;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
    
    &#39; May not be a mailitem.
    &#39;Dim outlookLatestItem As outlook.MailItem
    
    Dim subjectFilter As String
    Dim timeFilter As String
    
    Dim startDate As String
    Dim endDate As String
    
    &#39;Get the inbox from Outlook
    Set outlookInbox = Session.GetDefaultFolder(olFolderInbox)
    
    startDate = CStr(Date) &amp; &quot; &quot; &amp; &quot;00:00&quot;  &#39;Date can be replaced with any string Date
    Debug.Print &quot; startDate: &quot; &amp; startDate
    
    endDate = CStr(Date + 1) &amp; &quot; &quot; &amp; &quot;00:00&quot;  &#39;the same, it should be the previous Date +1
    Debug.Print &quot; endDate..: &quot; &amp; endDate
    
    &#39;subjectFilter = &quot;@SQL=urn:schemas:httpmail:subject&quot; &amp; &quot;&quot; &amp; &quot; ci_phrasematch &#39;Daily Water Consumption&#39;&quot;
    subjectFilter = &quot;@SQL=&quot; &amp; Chr(34) &amp; &quot;urn:schemas:httpmail:subject&quot; &amp; Chr(34) &amp; &quot; like &#39;%Daily Water Consumption%&#39;&quot;
    Debug.Print &quot; subjectFilter.: &quot; &amp; subjectFilter
    
    Set outlookRestrictItemsSubj = outlookInbox.Items.Restrict(subjectFilter)
    Debug.Print outlookRestrictItemsSubj.count
    
    timeFilter = &quot;[ReceivedTime] &gt;= &#39;&quot; &amp; startDate &amp; &quot;&#39; and [ReceivedTime] &lt; &#39;&quot; &amp; endDate &amp; &quot;&#39;&quot;
    Debug.Print &quot; timeFilter: &quot; &amp; timeFilter
    
    Set outlookRestrictItems = outlookRestrictItemsSubj.Restrict(timeFilter)
    Debug.Print outlookRestrictItems.count
    
    &#39;Check whether any items were found
    If outlookRestrictItems.count = 0 Then
        MsgBox &quot;No items were found!&quot;, vbExclamation
        Exit Sub
    End If
    
    &#39;Sort the filtered items by received time in descending order
    outlookRestrictItems.Sort Property:=&quot;[ReceivedTime]&quot;, Descending:=True
    
    &#39;Get the latest item from the filtered and sorted items
    Set outlookLatestItem = outlookRestrictItems(1)
    Debug.Print outlookLatestItem.subject
    Debug.Print outlookLatestItem.ReceivedTime
    
End Sub

huangapple
  • 本文由 发表于 2023年4月4日 16:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定