Link to home
Start Free TrialLog in
Avatar of Armin Braunstein
Armin BraunsteinFlag for Austria

asked on

Any reliable way for a MailItem_Reply event in Outlook's VBA?

Hi! I would need to hook in a reliable way to the Reply-Event with a VBA outlook macro.
The code I have currently is the following:

Private WithEvents CurrentExplorer As Explorer
Private WithEvents CurrentMailItem As MailItem
....
Private Sub Application_Startup()
  ..
  Set CurrentExplorer = Application.ActiveExplorer
  ..
End Sub

Private Sub CurrentExplorer_SelectionChange()
  ..
  Set CurrentMailItem = CurrentExplorer.Selection.Item(1)
  ..
End Sub

Private Sub CurrentMailItem_Reply(ByVal Response As Object, Cancel As Boolean)
  ... do something here..
End Sub

Open in new window


But that only works if the user really selects an item and then reply it.
If he right-clicks on a non-selected item, this event will not be called, because the selection did not change then.
But I also would need to know in such case that the user answers the email.

I already tried to hook into the Application_ItemLoad Event:
Private Sub Application_ItemLoad(ByVal Item As Object)
    If TypeOf Item Is Outlook.MailItem Then
        Set CurrentLoadedMailItem = Item
    End If
End Sub

Open in new window

and then hook this "CurrentLoadedMailItem.Reply"-Event. But the Reply-Event is never called in this way..

Is there any way I can get the event called always when the users answers an email?
Background is that I want to set a property to the new answer email, because for my macro I need to know the relation between answer and answered email.
I need to know for an answer email for which email that answer is.
Avatar of Qlemo
Qlemo
Flag of Germany image

Outlook already keeps that somewhere, otherwise it would not be able to show the corresponding icons and info for "forwarded/replied on ...".
Avatar of Armin Braunstein

ASKER

sure Outlook keeps the item somewhere, but how can I hook into the Reply-Event in a way, that I always get it called if the user clicks "Reply" - no matter how he do it.
The problem is that the CurrentExplorer_SelectionChange() event is not called, if the user directly clicks with the right mouse button on another mail item, that currently is not selected. Then the selection does not change (so the SelectionChange event is not called), but the user answers an email my VBA code does not know about.

Let me show a picture about what I mean:
User generated image
The selected email item is the email item I marked with a green box,
but I click with the right mouse on the other email item, which I marked with a red box,
which shows the context menu for that red-marked email item then.

So the SelectionChange()-Event happened for the green box item, but it does not happen for the red boxed item, because the selection did not change, so I have no chance to set the "CurrentMailItem" for the green boxed one.

And now the user clicks on "Reply" and I don't get an event, because I can not hook to it.

So my question is if there is any other way how I can hook to the Reply Event in a way, that I can also get it for such situations?
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wow, that indeed could help me a lot. I didn't know about the conversation history in Outlook.
I will dig into this further and see if that can solve my problem.
The example in C# is fine for me. The object model is anyway the same. Looks very helpful.
Thank you!
Thank you, your hint was very helpful. I already figured out how to deal with the conversation topic to solve my problem.