I have below excel macro which does the job of downloading the attachments from emails from outlook based on the subject lines which are listed in excel worksheet column, so what it does is, that it's looping through column B but unfortunately if the subject line of email doesn't match exactly with the cell value in column B then it wouldn't download the attachment of the same. I have few subject lines which consists of some code which change daily and hence I am entering only the identical part of that subject line which is same every day for e.g. subject line of the email is "Daily Report CRS YYYYMMDD 05422122", so in this case I will just mention the starting text of subject line .i.e. Daily Report CRS and i want the macro to search the email which consists of the aforementioned text. I have used the asterisk sign before and after the name in the code (see the highlighted line Red) but its not working.



Sub Downloademailattachementsfromexcellist()
Dim olApp As Object
Dim olNS As Object
Dim olItem As Object
Dim olRecip As Object
Dim olShareInbox As Object
Dim lRow As Integer
Dim olAttach As Object
Dim strPath As String
Dim strName As String
Dim xlSheet As Worksheet
Dim iRow as Integer
Const olFolderInbox As Integer = 6    ' I want to define the common shared mailbox over here...instead of my own personal box. Common mailbox name is IGT Team

    Set olApp = OutlookApp("outlook.application") ' this is the line which i tweaked to access outlook.
    Set olNS = olApp.GetNameSpace("MAPI")
    'The following two lines should get the shared folder, but without access to your setup I cannot test it

    'Set olRecip = olNS.CreateRecipient(olNS.CurrentUser.Address)    ' Owner's Name or email address ' can we define the name of the mailbox from an excel worksheet cell .i.e. ThisWorkbook.Sheets("Email Download").Range("F1").Value
    'Set olShareInbox = olNS.GetSharedDefaultFolder(olRecip, olFolderInbox)' how can we select a folder inside a shared mailbox and also define the name of the folder from a worksheet cell .i.e. ThisWorkbook.Sheets("Email Download").Range("G1").Value

    'so I have used the default inbox for testing
    Set olShareInbox = olNS.GetDefaultFolder(olFolderInbox)
    '-----------------------
    Set xlSheet = ActiveWorkbook.Sheets("Email Download")
    strPath = "C:\HP\" & xlSheet.Range("C1").value & "\"

    If olShareInbox.Items.restrict("[UNREAD]=True").Count = 0 Then
        MsgBox ("No Unread mails")
    Else
        CreateFolders strPath    'ensure the save path is present
        For Each olItem In olShareInbox.Items.restrict("[UNREAD]=True")
            lRow = xlSheet.Range("A" & xlSheet.Rows.Count).End(xlUp).Row    ' + 1
lRow = xlSheet.Range("B" & xlSheet.Rows.Count).End(xlUp).Row        
       For Each olItem In olShareInbox.Items.restrict("[UNREAD]=True")
            For iRow = 1 To lRow    'declare the variable iRow as integer
                'lRow = xlSheet.Range("A" & xlSheet.Rows.Count).End(xlUp).Row    ' + 1
                If InStr(1, olItem.Subject, "*" & xlSheet.Range("B" & iRow).value & "*") > 0 Then  ' each email subject line consists of date or some code which changes daily so I will just mention the unique part of the subject line which remains same daily.
                    If olItem.attachments.Count > 0 Then
                        For Each olAttach In olItem.attachments
                            strName = olAttach.FileName
                            olAttach.SaveAsFile strPath & strName
                            olItem.UnRead = False    ' Once the attachment is downloaded I want the macro to mark the mail as Read
                       Next olAttach
                    End If
                    Exit For 'subject found so stop looking
                End If
            Next iRow
        Next olItem

    End If
End Sub