Outlook emails, cannot mark as unread, Set oItem.Unread = False , does not work (1 Viewer)

bignose2

Registered User.
Local time
Today, 22:44
Joined
May 2, 2010
Messages
194
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
 

Isaac

Lifelong Learner
Local time
Today, 14:44
Joined
Mar 14, 2017
Messages
4,825
indent your code to make it readable.
use code tags when posting on awf.
half the people who read this post aren't even going to answer it, it's so ugly. do your part to post a good question that's easy to follow.

In order to use the Unread mailitem property, you wouldn't use a Set statement. "Set" is for variables.

and don't use on error resume next in your code. by doing that, you're telling VBA:
"Don't let me know if an error occurs. If one occurs, hide it from me so that I have no idea what's going on and can't solve my problem.
Don't do that.
 

bignose2

Registered User.
Local time
Today, 22:44
Joined
May 2, 2010
Messages
194
Hi,

Many thanks for your reply,

Really sorry is a bit of a mess, I just copied & pasted from the VBA, had quickly to go off an do something else and thought post it quickly without reviewing & hope for a reply rather than leave another 24 hours before chance to tidy up.

You were right about the Set. I never normally use that but had tried so many things thought worth a go & left it without thinking. Not sure why it did not worked before as 99% of my testing was without the set, but was the problem as you suggest.

Also the error handling was already there, was code I had copied directly and just wanted to get the basics working before really working on it.

Thanks again.
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:44
Joined
Sep 21, 2011
Messages
8,056
Google for Smart Indent and install that. It indents in a heartbeat. The indentation is not just for us, it makes it easier for you to read the code as well, or should do?
 

jdraw

Super Moderator
Staff member
Local time
Today, 17:44
Joined
Jan 23, 2006
Messages
13,684
Code indented to show improved readability.


Code:
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
 

Isaac

Lifelong Learner
Local time
Today, 14:44
Joined
Mar 14, 2017
Messages
4,825
Hi,

Many thanks for your reply,

Really sorry is a bit of a mess, I just copied & pasted from the VBA, had quickly to go off an do something else and thought post it quickly without reviewing & hope for a reply rather than leave another 24 hours before chance to tidy up.

You were right about the Set. I never normally use that but had tried so many things thought worth a go & left it without thinking. Not sure why it did not worked before as 99% of my testing was without the set, but was the problem as you suggest.

Also the error handling was already there, was code I had copied directly and just wanted to get the basics working before really working on it.

Thanks again.
Sure, understood.
But the on error resume next - I'd definitely take that out even to superficially play with it. Even especially in 'toying around', you want to generate every error you possibly can, so you know which line(s) of code are wrong.

So it's working now?
 

Users who are viewing this thread

Top Bottom