Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook VBA loop doesn't move all mail objects

Status
Not open for further replies.

coder1973

Programmer
Oct 6, 2009
2
US
hi - not sure why this code is only moving some emails in the drafts folder to another folder...any thoughts? it does move some emails, but then exits the for loop. I thought originally it only did half, but now it seems random.

thanks for any help!

Dim oOutlook As Outlook.Application
Dim oNs As Outlook.NameSpace
Dim oFldr As Outlook.MAPIFolder
Dim oFldrProcessed As Outlook.MAPIFolder
Dim oAttachments As Outlook.Attachments
Dim oAttachment As Outlook.Attachment
Dim iMsgCount As Integer

Dim oMessage As Outlook.MailItem

Dim iCtr As Long, iAttachCnt As Long

Dim sFileNames As String
Dim aFileNames() As String

Set oOutlook = New Outlook.Application
Set oNs = oOutlook.GetNamespace("MAPI")
Set oFldr = ThisOutlookSession.ActiveExplorer().Session.GetDefaultFolder(16)
Set oFldrProcessed = inBox.Folders("Old Drafts")
Debug.Print "Total Items: "; oFldr.Items.Count
Debug.Print "Total Unread items = " & oFldr.UnReadItemCount

For Each oMessage In oFldr.Items
With oMessage
oMessage.Move oFldrProcessed
End With
Next
 
When you delete elements from a collection in a loop, you should start at the end and work backwards. As you are deleting all the messages you have a little more flexibility, perhaps this will work:

Code:
[blue]While oFldr.items.count > 0
    oFldr.Items(1).Move oFldrProcessed
Wend[/blue]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top