Looping through items in Inbox and moving them

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
 
D

Dmitry Streblechenko \(MVP\)

The count of messages is changing as you loop through the collection. Use a
down loop (count to 1 step -1) instead of "for each".

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top