I am trying to loop through all emails in a mailbox and extract the attachment from each. The loop works as expected if I don't have the delete included. With the delete included it works for the first email and on the "next" statement exits the for loop, skipping the remaining email. I could separate out the delete to a new loop but that seems inefficient.
For Each itm In Inbox.Items
For Each objAtt In itm.Attachments
sEmailDate = Format(itm.ReceivedTime, "dd/mm/yyyy")
sDataDate = Format(DateAdd("d", -1, CDate(sEmailDate)), "dd/mm/yyyy")
sFileName = objAtt.Filename
sSubject = itm.Subject
'Check if the report was sent today
If sEmailDate = sTodayDate And sFileName = "Report.csv" Then
bToday = True
End If
'Look for Report file
If sFileName = "Report.csv" Then
'Save it to the save folder, as the DisplayName. This will overwrite previously saved copies of this file
objAtt.SaveAsFile saveFolder & "\" & "report" & sSubject & "_" & Format(sDataDate, "yyyymmdd") & ".csv"
If Err.Number = 0 Then
itm.Delete 'without this istwill loop correctly
iReportCount = iReportCount + 1
Else
GoTo ExitLoop
End If
End If
Next objAtt
Next itm