Outlook .items.restrict using two filters

前端 未结 2 1065
闹比i
闹比i 2021-01-22 04:13

I\'m using a script that opens an email and downloads its attachment. Right now I can either choose to download the most recent attachment on the most recent email:



        
相关标签:
2条回答
  • 2021-01-22 04:24
    @SQL=(Subject LIKE '%blah%') AND (ReceivedTime > '01/02/2015')
    
    0 讨论(0)
  • 2021-01-22 04:42

    It is a struggle to get the syntax for even one restrict. As indicated in the comment by Scott Holtzman, if you know each filter separately, you can filter twice.

    Option Explicit
    
    Sub CTEmailAttDownload()
    
        Const olFolderInbox As Integer = 6
        '~~> Path for the attachment
        Const AttachmentPath As String = "C:\TEMP\TestExcel"
    
        Dim oOlAp As Object
        Dim oOlns As Object
        Dim oOlInb As Object
    
        Dim oOlItm As Object
        Dim oOlAtch As Object
    
        Dim oOlResults As Object
        Dim oOlSubjectResults  As Object
        Dim strFilter As String
        Dim i As Long
    
        Dim x As Long
    
        Dim NewFileName As String
        NewFileName = "Daily Tracker " & format(Now, "dd-MM-yyyy")
    
        'You can only have a single instance of Outlook, so if it's already open
        'this will be the same as GetObject, otherwise it will open Outlook.
        Set oOlAp = CreateObject("Outlook.Application")
        Set oOlns = oOlAp.GetNamespace("MAPI")
        Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
    
        'No point searching the whole Inbox - just since yesterday.
        Set oOlResults = oOlInb.Items.Restrict("[ReceivedTime]>'" & format(Date - 1, "DDDDD HH:NN") & "'")
    
        strFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%test%'"
    
        Set oOlSubjectResults = oOlResults.Restrict(strFilter)
    
        If oOlSubjectResults.count = 0 Then
            Debug.Print "No emails found with applicable subject"
    
        Else
            'If you have more than a single attachment they'll all overwrite each other.
            'x will update the filename.
            x = 1
    
            For i = 1 To oOlSubjectResults.count
                Set oOlItm = oOlSubjectResults(i)
                If oOlItm.Attachments.count > 0 Then
                    Debug.Print oOlItm.Subject
                    For Each oOlAtch In oOlItm.Attachments
    
                        Debug.Print oOlAtch.DisplayName
    
                        If GetExt(oOlAtch.FileName) = "xlsx" Then
                            oOlAtch.SaveAsFile AttachmentPath & "\" & NewFileName & ".xlsx"
                        End If
                        x = x + 1
                    Next oOlAtch
                End If
            Next i
        End If
    
    ExitRoutine:
        Set oOlAp = Nothing
        Set oOlns = Nothing
        Set oOlInb = Nothing
    
        Set oOlResults = Nothing
        Set oOlSubjectResults = Nothing
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题