Looping Through Items in Outlook Inbox Then Moving Them

  • Thread starter Thread starter Jake Cole
  • Start date Start date
J

Jake Cole

When using this code to loop through ALL the items in an Outlook
Inbox, instead of moving every item, the code moves exactly half of
the mail items each time it is run

Here is the current code. I would like the code to move all inbox
items to the "completed" folder, -instead of just half, can anyone
tell me what the problem is with this code?! (This code is run from
within an access database.)

Dim mapiNameSpace As Outlook.NameSpace
Dim fldInbox As MAPIFolder
Dim newMail As MailItem
Dim fldMoveTo As MAPIFolder
Dim myTime
Dim olO As Outlook.Application
Dim sFolder As Outlook.Recipient

Set olO = New Outlook.Application
Set mapiNameSpace = olO.GetNamespace("MAPI")
Set sFolder = mapiNameSpace.CreateRecipient("Conference 2004")
Set fldInbox = mapiNameSpace.GetSharedDefaultFolder(sFolder,
olFolderInbox)
Set fldMoveTo = fldInbox.Folders("Completed")
cm = 0
MsgBox fldInbox.Items.Count
For Each newMail In fldInbox.Items
cm = cm + 1
newMail.Move fldMoveTo
Next
MsgBox cm & " Records Moved!"
End Sub

Thanks,
Jake
 
Hi,
Yes, I've heard of this happening when using the For Each construct.

What you should try is a loop using the Items.Count

Something like:

Dim i as Integer

For i = fldInbox.Items.Count -1 To 0 Step -1
fldInbox.Items(i).Move fldMoveTo
Next i

HTH
Dan Artuso, MVP
 
Back
Top