'What TypeOf are these conversation headers?

I am trying to file emails using a macro, but I am having issues if the user selects a conversation header. What is the typeOf of these headers?

Conversation Header

I am trying to obtain the parent folder when an email is selected, in order to determine whether the emails should be moved (or already have been). For a .MailItem I can get it using the following code:

Set selection = ActiveExplorer.selection
Set CurrentFolder = selection(1).Parent

But when only a conversation header is selected this returns an error "Run-time error '440': Array Index out of bounds."

When trying to use an if statement like this:

If TypeOf selection Is Outlook.MailItem Then
    Set CurrentFolder = selection(1).Parent
ElseIf TypeOf selection Is Outlook.ConversationHeader Then
    'Set CurrentFolder
Else
    'Return error
End If

the .ConversationHeader does not work, as in the ElseIf statement returns False.

What typeof should I be using? and what code should I then be using to find the parent folder?



Solution 1:[1]

The Selection property returns a Selection object, not a single item. You will need to loop through the selection items (use "for each" or "for" loop from 1 to Selection.Count using Selection.Item(index)) to get to the selected items.

You can see live Outlook objects with their properties, methods, and events using OutlookSpy (I am its author).

Solution 2:[2]

Selection.Item(index) will throw an exception if your selection is the ConversationHeader indicated in the supplied image.

I was unable to locate an actual type of object for that heading, but there are some ways around it. Once identified that it isn't a MailItem you can check ActiveExplorer.Selection.GetSelection(Outlook.OlSelectionContents.olConversationHeaders)

https://docs.microsoft.com/en-us/office/vba/api/outlook.selection.getselection

Most notably, they indicate

Calling GetSelection with olConversationHeaders as the argument returns a Selection object that has the Location property equal to OlSelectionLocation.olViewList.

If the current view is not a conversation view, or, if Selection.Location is not equal to OlSelectionLocation.olViewList , calling GetSelection with olConversationHeaders as the argument returns a Selection object with Selection.Count equal to 0.

Running a quick test of this

Dim oSelection = Selection
Set oSelection = ActiveExplorer.Selection.GetSelection(olConversationHeaders)
Print oSelect.Count 
--returns 1 when i have that odd header selected.  
--returns 0 when you have a mailItem within the header selected

Furthermore, take a look at How to move all messages in a conversation?

We can easily adjust the answers given to something along the lines of :

Set conversations = ActiveExplorer.Selection.GetSelection(Outlook.OlSelectionContents.olConversationHeaders)
Dim sItems As SimpleItems
    For Each Header In conversations
        Set Items = Header.GetItems()
        For i = 1 To Items.Count
            If TypeOf Items(i) Is Outlook.MailItem Then
            Debug.Print (Items(i).Parent)
            End If
        Next i
    Next Header
End Sub

Notably this only returned headers for emails in the selected group that were IN the folder the selection was made. I believe you'd have to further up the ante-by going into the getConversations and Conversations objects to travel down/upstream.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryan M
Solution 2