Outlook/Excel VBA - Not Loop Through All Msg

Viewed 14

I currently have the below VBA code which has the following issue: once I move the msg to the subfolder the For Next loop will not then go to the next msg in the original Outlook folder.

If I take out the msg.move olInboxFolder.Folders("Forms to do").Folders("Forms completed") line of code it does work. So this line seems to be the issue.

Dim msg as MailItem
Dim olSubFolder As MAPIFolder

Set olSubFolder = olInboxFolder.Folders("Forms to do")

For Each msg In olSubFolder.Items ' for each message in the subfolder

   'Do stuff

   msg.move olInboxFolder.Folders("Forms to do").Folders("Forms completed")

Next msg

Has anyone any ideas on how to resolve this?

Thanks in advance.

1 Answers

OK credit has to go to this post that helped me solve the problem:

Trying to move emails in a loop, but not all get moved in the first run

I changed my code to the following:

Dim msg as MailItem
Dim olSubFolder As MAPIFolder

Set olSubFolder = olInboxFolder.Folders("Forms to do")

Dim i As Integer

For i = olSubFolder.Items.Count To 1 Step -1 

    set msg = olSubFolder.Items(i)

   'Do stuff

   msg.move olInboxFolder.Folders("Forms to do").Folders("Forms completed")

Next 

I hope this helps someone else.

Related