Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-16-2018, 04:06 AM
knarfreppep knarfreppep is offline Looping through emails Windows 10 Looping through emails Office 2016
Novice
Looping through emails
 
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
  #2  
Old 04-07-2018, 07:21 AM
RetiredCHE RetiredCHE is offline Looping through emails Windows 10 Looping through emails Office 2013
Novice
 
Join Date: Jun 2016
Location: Alabaster, AL
Posts: 25
RetiredCHE is on a distinguished road
Default Loop in Reverse

I had a similar issue with a macro to delete all items in my Trash folder. The solution was to loop in reverse from the count of items to 1. Using that approach the macro worked the first time. I hope this will work for you.

RetiredCHe
Reply With Quote
  #3  
Old 04-07-2018, 08:03 AM
knarfreppep knarfreppep is offline Looping through emails Windows 10 Looping through emails Office 2016
Novice
Looping through emails
 
Join Date: Mar 2018
Posts: 2
knarfreppep is on a distinguished road
Default

Looping through my code x5 solved my problem, with execution time still excellent.

But I'll try/use your solution as I prefer it to my 'scatter gun' approach.

Thanks for taking an interest Alan!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping presentation elico PowerPoint 2 02-25-2016 05:58 AM
Looping several slides gabby PowerPoint 11 04-25-2015 03:05 AM
Looping through emails Need help on macro for looping kilosub Word VBA 31 07-27-2011 01:37 PM
Looping video tomasball PowerPoint 0 10-30-2010 02:38 PM
Unbreakable looping Dixon Outlook 0 09-23-2009 02:10 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:10 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft