Freesnöw
Freesnöw

Reputation: 32133

Get the MailItem specific to the reply in Outlook

I'm making an Outlook plugin that needs to change several properties of a MailItem when the user clicks "reply" to any email. I'm currently doing something like this:

Private Sub PrepareEmailForReply(ByVal MailItem As Outlook.MailItem, ByVal FromAddress As String)
    If Not ReplyDictionary.ContainsKey(MailItem.ConversationID) Then
        ReplyDictionary.Add(MailItem.ConversationID, FromAddress)
        AddHandler MailItem.Reply, Sub()
                                               InReply = True
                                   End Sub
    End If
End Sub

Private Sub CurrentExplorer_SelectionChange() Handles CurrentExplorer.SelectionChange
    Dim SelectedFolder As Outlook.MAPIFolder = Me.Application.ActiveExplorer().CurrentFolder
    Try
        If Me.Application.ActiveExplorer.Selection.Count > 0 Then
            Dim SelectedObject As Object = Me.Application.ActiveExplorer.Selection.Item(1)
            If (TypeOf SelectedObject Is Outlook.MailItem) Then
                Dim MailItem As Outlook.MailItem = TryCast(SelectedObject, Outlook.MailItem)

                If (CurrentEmail IsNot MailItem) Then
                    Output.AddInfo(MailItem.ConversationTopic)

                    CurrentEmail = MailItem
                    If (InReply) Then
                        Output.AddInfo("> In Reply")
                        If ReplyDictionary.ContainsKey(MailItem.ConversationID) Then
                            MailItem.Subject = "Testing Reply Email"
                        End If
                    Else
                        Output.AddInfo("> In MailItem")
                        For Each Recipient As Outlook.Recipient In MailItem.Recipients
                            Dim CurrentEmailAddress As String = Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress.ToLower.Trim()
                            If ListeningUsers.Contains(CurrentEmailAddress) Then
                                PrepareEmailForReply(MailItem, CurrentEmailAddress)
                                Exit For
                            End If
                        Next
                    End If
                    InReply = False
                End If
                End If
        End If
    Catch ex As Exception
    End Try
End Sub

Everything works fine, but the MailItem on the line that says `MailItem.Subject = "Testing Reply Email" doesn't actually correspond to the Reply email, so the subject doesn't get changed.

How do I get the MailItem for the reply email so that I can change the subject of it?

The ListeningUsers variable is a List(of String) that contains a list of what emails this is valid for. It contains the current user.

**EDIT: **

This is where I've added a few changes to the MailItem.Reply event call:

        AddHandler MailItem.Reply, Sub()
                                       Dim CurrentInspector As Outlook.Inspector = Globals.ThisAddIn.Application.ActiveInspector()
                                       Dim ReplyMailItem As Outlook.MailItem = TryCast(CurrentInspector.CurrentItem, Outlook.MailItem)
                                       If (ReplyMailItem IsNot Nothing) Then
                                           MsgBox("1: " & ReplyMailItem.Subject & " - " & ReplyMailItem.EntryID)
                                           Return
                                       End If

                                       CurrentInspector = CurrentExplorer.ActiveInlineResponse
                                       ReplyMailItem = TryCast(CurrentInspector.CurrentItem, Outlook.MailItem)
                                       If (ReplyMailItem IsNot Nothing) Then
                                           MsgBox("2: " & ReplyMailItem.Subject & " - " & ReplyMailItem.EntryID)
                                       End If
                                   End Sub

The first msgbox will appear if I open an email in an external window and then click reply, however, if I click reply to an email inside of Outlook (not a popup window) then the second msgbox should appear, but it doesn't.

Upvotes: 0

Views: 2359

Answers (1)

Eric Legault
Eric Legault

Reputation: 5834

Getting the reply email is not an easy thing to do. I've bugged the Outlook product team for many years now to include a ReplyItem property on the MailItem object but they haven't listened.

The problem is that the user can reply to a selected email OR an open email. You can always monitor Inspectors.NewInspector and check for a blank MailItem.EntryID value for the new emails and ensure that MailItem.Recipients.Count > 0. Then you'll know it's a reply, and then you'll have to get Explorer.Selection OR loop through Inspectors (to find open email windows). Then you'll have to match ConversationID on the selected/open email with the new compose email.

Upvotes: 1

Related Questions