Outlook 2010 Add-in

Zamdrist

Member
Joined
May 21, 2010
Messages
13
Programming Experience
5-10
Developing an Outlook 2010 add-in to capture the Reply All functionality, giving the user a prompt and opportunity to back out. The code I have (included) is close but not quite there. Hopefully someone can help. There are three ways to Reply All to a message. You can click the Reply All button from Office Ribbon, you can right-click on the message and choose Reply All, or you can hit Reply All from in the opened message itself. The code I've written works. Sort of. If you right-click the message it always works, but any of the other methods only work after you've opened the message.

I'm clearly not capturing all of the events I need to, and possibly in not in the proper way too. Any advice, suggestions or help would be greatly appreciated. Thanks!


VB.NET:
Imports Microsoft.Office.Interop.Outlook

Public Class ThisAddIn

    Public WithEvents olkEx As Outlook.Explorer
    Public WithEvents olkMailItem As Outlook.MailItem
    Public WithEvents olkExplorers As Outlook.Explorers

    Private Sub ThisAddIn_Startup() Handles Me.Startup

        Dim myOL As Outlook.Application
        myOL = Globals.ThisAddIn.Application
        olkEx = myOL.ActiveExplorer
        olkMailItem = olkEx.Selection.Item(1)

    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    End Sub

    Private Sub olkEx_Activate() Handles olkEx.Activate

        If olkEx.Selection.Count > 0 Then
                olkMailItem = olkEx.Selection.Item(1)
        End If

    End Sub

    Private Sub olkMailItem_ReplyAll(ByVal Response As Object, ByRef Cancel As Boolean) Handles olkMailItem.ReplyAll

        If olkMailItem.Class = Outlook.OlObjectClass.olMail Then

            If MsgBox("Are you sure you want to Reply To All?", _
            vbQuestion + vbYesNo, "Verify Reply to All (M&G)") = vbNo Then
                Cancel = True
            End If
        End If

    End Sub

End Class
 
In relation to Selection I would go for this event: Explorer.SelectionChange Event (Outlook)
Also note that selection.Item may not be of type MailItem, see How to: Determine the Current Outlook Item

Thank you John. I did indeed go back and account for the selection change event in my code, and that does indeed work. In fact, my add-in works perfectly, but only in a perfect test environment if you will.

Once you begin using Outlook in a more typical everyday fashion, problems arise. Say for example you open an e-mail, leave it open and go back to the main Outlook window. Later you go back to the opened e-mail, and the Reply All code I've written doesn't catch the event. The reason I know this doesn't work properly is because the object variable for the MailItem object is getting unset or reset based on event-driven actions by the user.

It is kind of a quandary. How do I ensure my object variable remains set and ensure I catch the Reply All event of the MailItem, while accounting for the user's unpredictable actions.

It has been suggested elsewhere I may need to make use of the extended PIA events and delegates in order to make this work. Looks far more complicated that what should be necessary but that may just be the case. I am not sure. While I am not new to VB and VBA coding, I am fairly new to the Outlook object library (Outlook Object Model Reference).

I know how to add events and use them, but I don't necessarily know how to delegate them, or otherwise re-purpose them, if indeed that is what I need to do even.

There are vendors out there (two maybe three) selling an add-in that in fact does this, but for ridiculous prices (~$4000 for 100+ users) and my employer is hoping I can develop this myself.

Any guidance, help, thoughts or leads would be greatly appreciated. Thanks!
 

Latest posts

Back
Top