Darren Bartrup
Well-known Member
- Joined
- Mar 13, 2006
- Messages
- 1,297
- Office Version
- 365
- Platform
- Windows
I'm having a slight problem processing Outlook emails and marking them as unread once they've been dealt with.
The code below asks for an Outlook folder and then shows in the immediate window how many unread emails there are today and the subject of each email. This returns 65 for me.
Note the commented out line 'oItem.UNREAD = False at the bottom of the code.
If I let this line execute the procedure returns 33 emails - so each email is processed, marked as unread and then removed from the filter but the filter isn't keeping track of which emails to remove. It just removes the one at the top of the list I guess and causes a similar problem to deleting rows in Excel where you must start at the bottom and move up or risk deleting the wrong row.
Is there a way to process all unread emails and mark them as read?
I tried putting mFolderSelected.items.Restrict(sFilter).unread = False after the loop, but as I expected it didn't work.
Any help greatly appreciated.
The code below asks for an Outlook folder and then shows in the immediate window how many unread emails there are today and the subject of each email. This returns 65 for me.
Note the commented out line 'oItem.UNREAD = False at the bottom of the code.
If I let this line execute the procedure returns 33 emails - so each email is processed, marked as unread and then removed from the filter but the filter isn't keeping track of which emails to remove. It just removes the one at the top of the list I guess and causes a similar problem to deleting rows in Excel where you must start at the bottom and move up or risk deleting the wrong row.
Is there a way to process all unread emails and mark them as read?
I tried putting mFolderSelected.items.Restrict(sFilter).unread = False after the loop, but as I expected it didn't work.
Code:
Public Sub OutlookTest()
Dim olApp As Object
Dim nNameSpace As Object
Dim mFolderSelected As Object
Dim oItem As Object
Dim sFilter As String
Dim i As Long
Set olApp = CreateObject("Outlook.Application")
Set nNameSpace = olApp.GetNamespace("MAPI")
Set mFolderSelected = nNameSpace.PickFolder
sFilter = "[ReceivedTime] >= '" & Format(Date, "ddddd h:nn AMPM") & "' AND [UNREAD]=TRUE"
Debug.Print mFolderSelected.items.Restrict(sFilter).Count
i = 0
For Each oItem In mFolderSelected.items.Restrict(sFilter)
i = i + 1
Debug.Print i & " : " & oItem.Subject
'oItem.UNREAD = False
Next oItem
End Sub
Any help greatly appreciated.