Solved

Outlook VBA not all emails moved

Posted on 2014-11-25
1
94 Views
Last Modified: 2014-12-04
Each time I run this code, only some emails are moved.  I have to run it 3 to 5 times to get all of them moved.

 For Each oItem In oFolder.Items
    If TypeName(oItem) = "MailItem" Then
        If Left(oItem.Subject, 31) = "Information about your order (#" And oItem.ReceivedTime > #11/23/2014# Then
            oItem.Move oDestFolder
            Debug.Print oItem.Subject
        End If
    End If
 Next

Open in new window

0
Comment
Question by:rrhandle8
1 Comment
 
LVL 18

Accepted Solution

by:
SimonAdept earned 500 total points
Comment Utility
Try moving them in reverse order. When you move items out of a collection it stuffs the indexing up.

See code below... just written here, not tested but should work. I do this on a regular basis with Outlook mailitems.

itemCount = oFolder.items.count
 For i from itemcount to 1 step -1
set oItem = ofolder.items(i)
    If TypeName(oItem) = "MailItem" Then
        If Left(oItem.Subject, 31) = "Information about your order (#" And oItem.ReceivedTime > #11/23/2014# Then
            oItem.Move oDestFolder
            Debug.Print oItem.Subject
        End If
    End If
 Next i

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Outlook Free & Paid Tools
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now