【问题标题】:Creating dynamic filter using date range entered by user使用用户输入的日期范围创建动态过滤器
【发布时间】:2021-04-01 13:52:04
【问题描述】:

我有一个宏来过滤来自 Outlook 收件箱的电子邮件,并列出过滤结果的附件。

我需要给用户一种输入日期或日期范围的过滤参数的方法。

对于不太懂SQL的人,有没有比较简单的格式?

Sub SaveAttachments()
    Dim ol As Outlook.Application
    Dim ns As Outlook.Namespace
    Dim fol As Outlook.Folder
    Dim i As Object
    Dim mi As Outlook.MailItem
    Dim Inbox As MAPIFolder
    Dim strDate As String
           
    'strDate = InputBox("Enter Date in format dd-Mmm-yyyy", "User Date", Format(Now(), "dd-Mmm-yyyy"))
       
    Set ol = New Outlook.Application
    Set ns = ol.GetNamespace("MAPI")
    Set fol = ns.Folders("GCMNamLogs").Folders("Inbox")
        
    'For Each i In fol.Items.Restrict("@SQL=urn:schemas:httpmail:subject LIKE '%strDate%'")
    For Each i In fol.Items.Restrict("@SQL=urn:schemas:httpmail:subject LIKE '%30-Mar-2021%'")
        If i.Class = olMail Then
            Set mi = i
            'For Each at In mi.Attachments
                Debug.Print mi.Subject
            'Next at
        End If
    Next i
    
End Sub

我用 strDate 替换过滤器的尝试失败了。

我想知道正确的格式是什么,或者我想知道如何实现这一点的建议。

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    您需要将输入的日期字符串与搜索条件连接起来:

    For Each i In fol.Items.Restrict("@SQL=urn:schemas:httpmail:subject LIKE '%" & strDate & "%'")
    

    更多信息请参见How can I concatenate strings in VBA?

    【讨论】:

    • 完美!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多