0

I've a macro to filter emails from an Outlook Inbox, and list the attachments of the filtered results.

I need to give the user a way to input a filter parameter of a date or a date range.

Is there a relatively simple format for those who don't understand SQL well?

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

My attempts to substitute the filter with strDate failed.

I'd like to know what the proper format would be, or suggestions for how I might accomplish this.

Community
  • 1
  • 1

1 Answers1

1

You need to concatenate the entered date string with a search criteria:

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

See How can I concatenate strings in VBA? for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45