View Single Post
 
Old 03-16-2018, 04:06 AM
knarfreppep knarfreppep is offline Windows 10 Office 2016
Novice
 
Join Date: Mar 2018
Posts: 2
knarfreppep is on a distinguished road
Default Looping through emails

Hi all!

Code below loops through every email in Inbox and is supposed to move those pre-processed so as to have {P} at the beginning of their Subject to an Archived folder under Inbox.

Problem is, not all such emails are archived in a 'run' ... one may need to run the code two or three times to move all emails.

I'd hoped DoEvents would help ... it didn't.

I realise I can just loop through the code (' For bytLoop = 1 to 5) but does anyone know why the code might be failing or a better way please?

Code:
    Set appApplication = CreateObject("Outlook.Application")
    Set nspNameSpace = appApplication.GetNamespace("MAPI")
    Set objFolder_Source = nspNameSpace.GetDefaultFolder(olFolderInbox)
    Set objFolder_Archived = objFolder_Source.Folders.Add("Archived")
    Set objFolder_Archived = objFolder_Source.Folders("Archived")
    Set objFolder_OldItems = objFolder_Source.Folders.Add("Old Items")
    Set objFolder_OldItems = objFolder_Source.Folders("Old Items")
'    For bytLoop = 1 to 5
    For Each varMailItem In objFolder_Source.Items
        If Left(varMailItem.Subject, 3) = "{P}" Then
            varMailItem.Move objFolder_Archived
            DoEvents
        End If
'    Next
    Next varMailItem
Reply With Quote