Hi,
Have code below copied from others, I added the, Set oItem.Unread = False (with or without the "set", makes not difference)
It does not mark as unread & still in Outlook & server as unread.
I have seen others that loop through separately afterwards, just to clear but not tried but also not keen (perhaps unfounded concern)
I wondered if an email came in at that instant, could it mark as unread without being seen, not sure if the way coded below would prevent that.
but just felt below should work and seen it done like that.
Sub Outlook_ExtractMessages()
Dim oOutlook As Object 'Outlook.Application
Dim oNameSpace As Object 'Outlook.Namespace
Dim oFolder As Object 'Outlook.folder
Dim oItem As Object
Dim oPrp As Object
Const olFolderInbox = 6
Const olMail = 43
On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application") 'Bind to existing instance of Outlook
If Err.Number <> 0 Then 'Could not get instance, so create a new one
Err.Clear
Set oOutlook = CreateObject("Outlook.Application")
End If
On Error GoTo Error_Handler
Set oNameSpace = oOutlook.GetNamespace("MAPI")
' Set oFolder = oNameSpace.GetDefaultFolder(olFolderInbox)
' Set oFolder = oOutlook.ActiveExplorer.CurrentFolder 'Process the currently selected folder
' Set oFolder = oNameSpace.PickFolder 'Prompt the user to select the folder to process
Set oFolder = oNameSpace.Folders("MainEmail@gmail.com").Folders("inbox") ' not real email addr.
On Error Resume Next
For Each oItem In oFolder.items
If oItem.Unread = True Then
With oItem
If .Class = olMail Then
Debug.Print .Subject, .Sender, .SentOn, .ReceivedTime
For Each oPrp In .ItemProperties
' Debug.Print , oPrp.Name, oPrp.value
Next oPrp
End If
End With
End If
Set oItem.Unread = False ' mark as read ' =========== DOES NOT WORK ==============
Next oItem
Error_Handler_Exit:
On Error Resume Next
If Not oPrp Is Nothing Then Set oPrp = Nothing
If Not oItem Is Nothing Then Set oItem = Nothing
If Not oFolder Is Nothing Then Set oFolder = Nothing
If Not oNameSpace Is Nothing Then Set oNameSpace = Nothing
If Not oOutlook Is Nothing Then Set oOutlook = Nothing
Exit Sub
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: Outlook_ExtractMessages" & vbCrLf & _
"Error Description: " & Err.DESCRIPTION & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Sub
Have code below copied from others, I added the, Set oItem.Unread = False (with or without the "set", makes not difference)
It does not mark as unread & still in Outlook & server as unread.
I have seen others that loop through separately afterwards, just to clear but not tried but also not keen (perhaps unfounded concern)
I wondered if an email came in at that instant, could it mark as unread without being seen, not sure if the way coded below would prevent that.
but just felt below should work and seen it done like that.
Sub Outlook_ExtractMessages()
Dim oOutlook As Object 'Outlook.Application
Dim oNameSpace As Object 'Outlook.Namespace
Dim oFolder As Object 'Outlook.folder
Dim oItem As Object
Dim oPrp As Object
Const olFolderInbox = 6
Const olMail = 43
On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application") 'Bind to existing instance of Outlook
If Err.Number <> 0 Then 'Could not get instance, so create a new one
Err.Clear
Set oOutlook = CreateObject("Outlook.Application")
End If
On Error GoTo Error_Handler
Set oNameSpace = oOutlook.GetNamespace("MAPI")
' Set oFolder = oNameSpace.GetDefaultFolder(olFolderInbox)
' Set oFolder = oOutlook.ActiveExplorer.CurrentFolder 'Process the currently selected folder
' Set oFolder = oNameSpace.PickFolder 'Prompt the user to select the folder to process
Set oFolder = oNameSpace.Folders("MainEmail@gmail.com").Folders("inbox") ' not real email addr.
On Error Resume Next
For Each oItem In oFolder.items
If oItem.Unread = True Then
With oItem
If .Class = olMail Then
Debug.Print .Subject, .Sender, .SentOn, .ReceivedTime
For Each oPrp In .ItemProperties
' Debug.Print , oPrp.Name, oPrp.value
Next oPrp
End If
End With
End If
Set oItem.Unread = False ' mark as read ' =========== DOES NOT WORK ==============
Next oItem
Error_Handler_Exit:
On Error Resume Next
If Not oPrp Is Nothing Then Set oPrp = Nothing
If Not oItem Is Nothing Then Set oItem = Nothing
If Not oFolder Is Nothing Then Set oFolder = Nothing
If Not oNameSpace Is Nothing Then Set oNameSpace = Nothing
If Not oOutlook Is Nothing Then Set oOutlook = Nothing
Exit Sub
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: Outlook_ExtractMessages" & vbCrLf & _
"Error Description: " & Err.DESCRIPTION & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Sub