For Each loop: Some items get skipped when looping through Outlook mailbox to delete items

Likely cause: When you do this InboxMsg.Move, all of the messages in your inbox after the one that was moved are bumped up by one position in the list. So you end up skipping some of them. This is a major annoyance with VBA’s For Each construct (and it doesn’t seem to be consistent either).

Likely solution: Replace

For Each InboxMsg In Inbox.Items

with

For i = Inbox.Items.Count To 1 Step -1 'Iterates from the end backwards
    Set InboxMsg = Inbox.Items(i)

This way you iterate backward from the end of the list. When you move a message to deleted items, then it doesn’t matter when the following items in the list are bumped up by one, because you’ve already processed them anyway.

Leave a Comment