1

I use a for each loop to loop thro all the emails on the filter but I always have some emails left behind.

I need to run this script multiple times to clean it.

How do i fix it?

Dim objOutlook As Outlook.Application
Dim objNamespace As Outlook.NameSpace
Dim objSourceFolder As Outlook.MAPIFolder
Dim objDestFolder As Outlook.MAPIFolder
Dim InboxMsg As Object
Dim Inbox As Outlook.Folder
Set objOutlook = Application
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set Inbox = objNamespace.GetDefaultFolder(olFolderInbox)
Dim myRestrictItems As Outlook.Items
 Dim filter As String

'Set Items = Inbox.Items
  filter = "[SenderEmailAddress] = 'tmeadmin@dm5cbs.on.rogers.ca'  or [SenderEmailAddress] = 'tmeadmin@dm5cbr.on.rogers.ca'" _
  & "or [SenderEmailAddress] = 'tmeadmin@dcobyj-030.qc.rogers.ca'" _
  & "or [SenderEmailAddress] = 'tmeadmin@dmobeo-030.on.rogers.ca'"
 Set myRestrictItems = Inbox.Items.Restrict(filter)
 MsgBox (myRestrictItems.Count)
 Set MoveToFolder = objNamespace.Folders("archive").Folders("INBOX").Folders("rogers_ALERTS")
 For Each mail In myRestrictItems
 mail.Move (MoveToFolder)
 Next


End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
junkone
  • 1,427
  • 1
  • 20
  • 45

1 Answers1

1

Of course - you are modifying the collection in the iterator. Use a down "for" loop from Count down to 1 (step -1)

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78