Apologies...and I am extremely sorry for repeatedly asking the question. I really appreciate the help which I got so far. can you please help me with the point 2 to run the second loop. I
2.The code doesn't loop through the items in Column B. It loops through the messages and compares them with the lastrow of column B. If you want to compare them with all the items in Column B then you need to set a second loop to check each relevant cell.
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
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
Dim Rng As Range
Set Rng = Range("B3", Range("B1").End(xlDown))
Counter = Rng.Count
For i = 1 To Counter
If InStr(1, olItem.Subject, xlSheet.Range("B" & lRow).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
End If
Next olItem
Next i
End If
End Sub