0

I'm trying to go through all unread emails in my inbox, find ones that contain a certain subject line, mark them as unread and move to a folder.

It works for some emails but not all unread emails so I end up having to click the button I created for the macro multiple times and i'm not sure why.

Here is the macro:

Sub MoveAlerts()

On Error Resume Next
Set oOutlook = CreateObject("Outlook.Application")
Set oNamespace = oOutlook.GetNamespace("MAPI")
Set oFolderSrc = oNamespace.GetDefaultFolder(olFolderInbox)
Set oFolderDst = oFolderSrc.Folders("@Alerts")
Set oFilteredItems = oFolderSrc.Items.Restrict("[UnRead] = True")
Dim strSubject As String
For Each oMessage In oFilteredItems
      If InStr(oMessage.Subject, "high temperature") Or InStr(oMessage.Subject, "Temperature: High") Then
        oMessage.UnRead = False
        oMessage.Move oFolderDst
      End If
Next
End Sub
0m3r
  • 11,696
  • 15
  • 30
  • 65
  • Possible duplicate of [For Each loop: Some items get skipped when looping through Outlook mailbox to delete items](https://stackoverflow.com/questions/10725068/for-each-loop-some-items-get-skipped-when-looping-through-outlook-mailbox-to-de) – niton Aug 28 '20 at 17:36
  • Tried modifying mine with both options and neither worked. – edwardo Aug 28 '20 at 19:09
  • 1
    Downward for loop is the way to go. Please show your updated code. – Dmitry Streblechenko Aug 29 '20 at 01:22
  • downward + set the filter for both unread & subject, and also make sure to save after `oMessage.UnRead = False` before move – 0m3r Aug 29 '20 at 07:02
  • I edited in correctly and tried again with downward loop and it worked. Thank you all. – edwardo Sep 01 '20 at 15:31

0 Answers0