0
votes

Does anybody know how to move a set of emails from one outlook folder to another based on som qualifying function (i.e. not all emails) ? I have the following code which works ok for the first qualifying email, but not the second or third... Dim objOL As Outlook.Application = New Outlook.Application
Dim objNS As Outlook.NameSpace = objOL.GetNamespace("MAPI")
olSourceFolder = objNS.Folders(MailboxIndex).Folders(3) ' (3 by way of example)
Dim myItems As Outlook.Items = olSourceFolder.Items

Dim i As Integer = 1  
Dim M As Outlook.MailItem  

Do While i <= myItems.Count  
  M = myItems(i)  
  If qualifyingFunction(M)  
    M.Move(olDestinationFolder)  
  Else 
    i = i + 1  
  End If 
Loop 

After a "move", M will not be correctly assigned the next time around I am guessing I dont really know how I should be iterating through the emails ?? Thanks.

1

1 Answers

0
votes

Use a down "for" loop

for i = myItems.Count to 1 step -1
  M = myItems(i)  
  If qualifyingFunction(M)  
    M.Move(olDestinationFolder)   
  End If 
Loop