Restrict Outlook Items by Date

后端 未结 2 1667
广开言路
广开言路 2020-12-10 20:45

I have an Outlook macro that filters email objects by date and returns items based on an array.

The filter for today is the following:

sfilter = \"[R         


        
相关标签:
2条回答
  • 2020-12-10 21:04

    You can find yesterday's mail with two separate Restricts.

    Private Sub EmailYesterday()
    
    Dim oOlInb As Folder
    Dim oOlItm As Object
    
    Dim oOlResults As Object
    Dim i As Long
    
    Dim sFilter As String
    Dim sFilter2 As String
    
    Set oOlInb = Session.GetDefaultFolder(olFolderInbox)
    
    'Filter recent - Lower Bound of the range
    sFilter = "[ReceivedTime]>'" & format(Date - 1, "DDDDD HH:NN") & "'"
    
    Debug.Print vbCr & sFilter
    Set oOlResults = oOlInb.Items.Restrict(sFilter)
    Debug.Print oOlResults.count & " items."
    
    If oOlResults.count > 0 Then
        For i = 1 To oOlResults.count
            Set oOlItm = oOlResults(i)
            Debug.Print oOlItm.Subject & " - " & oOlItm.ReceivedTime
        Next i
    End If
    
    ' Filter range - Upper Bound
    sFilter2 = "[ReceivedTime]<'" & format(Date, "DDDDD HH:NN") & "'"
    
    Debug.Print vbCr & sFilter; " AND " & sFilter2
    
    Set oOlResults = oOlResults.Restrict(sFilter2)   ' Restrict the Lower Bound result
    Debug.Print oOlResults.count & " items."
    
    If oOlResults.count > 0 Then
        For i = 1 To oOlResults.count
            Set oOlItm = oOlResults(i)
            Debug.Print oOlItm.Subject & " - " & oOlItm.ReceivedTime
        Next i
    End If
        
    ExitRoutine:
        Set oOlInb = Nothing
        Set oOlResults = Nothing
        Debug.Print "Done."
        
    End Sub
    
    0 讨论(0)
  • 2020-12-10 21:23

    Yesterday date could be filtered as below

    oOlResults.Restrict("@SQL=%yesterday(""urn:schemas:httpmail:datereceived"")%")
    

    The same for today or this month.

     oOlResults.Restrict("@SQL=%today(""urn:schemas:httpmail:datereceived"")%")
     oOlResults.Restrict("@SQL=%thismonth(""urn:schemas:httpmail:datereceived"")%")
    

    More info here

    0 讨论(0)
提交回复
热议问题