【问题标题】:Restrict method filter with multiple date condition限制具有多个日期条件的方法过滤器
【发布时间】:2018-01-09 06:11:54
【问题描述】:

我正在尝试使用 Excel VBA 过滤掉我的 Outlook 邮件收件箱,然后在满足条件时最终发送电子邮件。

完整条件是:如果 Outlook 收件箱包含日期范围内(过去 7 天)和来自发件人(动态发件人电子邮件)的主题。

我已经完成了sub sendEmails(),现在正在努力过滤邮件。

我现在已经成功过滤掉了我正在查看的主题的代码。 但是,在尝试将日期范围包含在过滤器中之后, 搞砸了。

第一个问题:过滤器给了我

运行时错误 13:类型不匹配。

我知道会发生这种情况,因为过滤器包含 StringDate 值,所以我尝试将类型更改为 variant 但仍然遇到错误。

另一个问题是我跟随this post 尝试添加日期条件。和this post 应用日期过滤器。 如果有经验的人可以纠正我的错误,那么非常感谢许多错误。 (还没跑到那里,但只是有强烈的感觉会遇到错误)

这是我第一次使用它,所以请放轻松。

Sub Search_Inbox()

Dim myOlApp As New Outlook.Application
Dim objNamespace As Outlook.Namespace
Dim objFolder As Outlook.MAPIFolder
Dim filteredItems As Outlook.Items
Dim itm As Object
Dim Found As Boolean
Dim Filter As Variant

Dim tdyDate As String
Dim checkDate As Date
tdyDate = Format(Now(), "Short Date")
checkDate = DateAdd("d", -7, tdyDate) ' DateAdd(interval,number,date)

Set objNamespace = myOlApp.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

'https://msdn.microsoft.com/en-us/library/aa579702(v=exchg.80).aspx
Filter = "@SQL=" & Chr(34) & "(urn:schemas:httpmail:subject" & Chr(34) & " like 'Reminder on Subject' &" _
                     And Chr(34) & "urn:schemas:httpmail:datereceived >= & checkDate &  " _
                     And Chr(34) & "urn:schemas:httpmail:datereceived >= & tdyDate &"

Set filteredItems = objFolder.Items.Restrict(Filter)

If filteredItems.Count = 0 Then
    Debug.Print "No emails found"
    Found = False
Else
    Found = True
    ' this loop is optional, it displays the list of emails by subject.
    For Each itm In filteredItems
     Debug.Print itm.Subject
    Next
End If


'If the subject isn't found:
If Not Found Then
    'NoResults.Show
Else
   Debug.Print "Found " & filteredItems.Count & " items."

End If
Set myOlApp = Nothing
End Sub

【问题讨论】:

    标签: excel vba outlook outlook-filter


    【解决方案1】:

    假设您只是在添加日期文件时遇到问题,您可以找到一些有用的东西here
    现在将它应用到您的代码中,您必须用单引号 (') 将日期括起来。

    虽然日期和时间通常以日期格式存储,但 Find 和 Restrict 方法要求将日期和时间转换为字符串表示形式。

    Dim tdyDate As String, checkDate As String
    
    tdyDate = "'" & Format(Date, "Short Date") & "'"
    checkDate = "'" & Format(Date - 7, "Short Date") & "'"
    

    你也可以试试:

    tdyDate = Format(Date, "\'ddddd\'") '/* until todays date */
    checkDate = Format(Date - 7, "\'ddddd\'") '/* I suppose you are filtering 7 days ago? */
    

    然后构建你的过滤器:

    eFilter = "@SQL= (urn:schemas:httpmail:subject Like 'Reminder on Subject'" & _
              " And urn:schemas:httpmail:datereceived >= " & checkDate & _
              " And urn:schemas:httpmail:datereceived <= " & tdyDate & ")"
    

    注意:我使用eFilter 而不是Filter 作为变量,因为它是VBA 中的保留字。

    这会给你:

    @SQL= (urn:schemas:httpmail:subject Like 'Reminder on Subject' And urn:schemas:httpmail:datereceived >= '1/2/2018' And urn:schemas:httpmail:datereceived

    【讨论】:

    • 谢谢,解释得很好。在搜索 String(subject) 的信息时,我几次遇到那篇文章,但没有阅读日期的详细信息。 :( 我想这对我来说是额外的学习内容。
    • @Max Cool,希望它现在可以工作。我没有测试过代码,但你提到它已经在工作了,除了日期,所以我检查了文档的内容并应用它。
    猜你喜欢
    • 2022-01-21
    • 2021-08-15
    • 2013-01-18
    • 2012-10-10
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 2019-10-10
    相关资源
    最近更新 更多