Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29758

Opening Outlook conversations without marking them as read using the VBA MarkAsUnread function

$
0
0

I am struggling for days on the following challenge:

Assumed you grouped your Outlook messages by conversations -- How to prevent any mail from a conversation to get marked read when double clicking on a conversation's main header?

Code I came up with so far:

  1. I use Application_ItemLoad to get the object of any selected mail
  2. Then, myItem_Read to store the selected mail's UnRead property because it's not accessible yet at the time of ItemLoad
  3. Finally, I listen on the PropertyChange event to change back any read mail to unread. However, in the else branch below the expression mySelection.Item(1).GetConversation.MarkAsUnread fails unexpectedly. From my understanding, mySelection.Item(1) selects the Conversation Header object. Then I try to obtain its Conversation object using GetConversationand call the MarkAsUnread method which should in theory mark all the conversation's messages as unread again. Just theory... And I don't know why. Your help is highly appreciated.

1.

Public WithEvents myItem As Outlook.mailItem

Private Sub Application_ItemLoad(ByVal Item As Object)
    If EventsDisable = True Then Exit Sub
    If Item.Class = olMail Then
            Set myItem = Item
     End If
End Sub

2.

Private Sub myItem_Read()
    If EventsDisable = True Then Exit Sub
    unReadWhenSelected = myItem.UnRead
End Sub

3.

Private Sub myItem_PropertyChange(ByVal Name As String)
    Dim mySelection As Selection
    Dim oConvHeader As Outlook.ConversationHeader
    Dim oConv As Outlook.Conversation

    If EventsDisable = True Then Exit Sub

    If Name = "UnRead" Then
        If unReadWhenSelected = True And myItem.UnRead = False Then

            Set mySelection = Outlook.ActiveExplorer.Selection.GetSelection(Outlook.OlSelectionContents.olConversationHeaders)


            If mySelection.Count = 0 Then

                myItem.UnRead = True
                myItem.Save

            Else         
                mySelection.Item(1).GetConversation.MarkAsUnread

            End If

        End If
    End If    
End Sub

Whole story for interested readers:
Let's say you wanted to open a conversation by double clicking its header without the conversation's elements getting marked as read. How to accomplish that in the most elegant way? I want to use Outlook E-Mails as tasks and change the meaning of a reading a mail to finishing a task. Thus, I use search folders with the option "only show unread messages". As soon as I finish a task, I just mark it read with a macro. But for all the other cases where I just want to read the mails etc. they need to remain unread.

So far, I managed to write a macro to accomplish this for single E-Mail messages which are not part of conversations. And when it comes to conversations, this macro works for all its elements - but not for the first one also known as the main conversation's header entry.

Edit:

Added proof-of-concept code for actually marking all emails in a conversation as read. Doesn't work in my scenario, though. So, why is this not working in my example code above?

Sub Testorino()

    Dim mySelection As Selection
    Set mySelection = Outlook.ActiveExplorer.Selection.GetSelection(Outlook.OlSelectionContents.olConversationHeaders)
    mySelection.Item(1).GetConversation.MarkAsUnread

End Sub

Viewing all articles
Browse latest Browse all 29758

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>