How do I catch Outlook 2000 MailItem Reply Event

R

Russell Mangel

I am unable to catch the MailItem Reply Event:

Can someone help? I have been hacking on this for 2 days.

I am using VB6 (COM-ADDIN)
Outlook 2000 (No SR installed)

' Begin Code
Option Explicit
Dim WithEvents oApplication As Outlook.Application
Dim WithEvents oNameSpace As Outlook.NameSpace
Dim WithEvents colInsp As Outlook.Inspectors
Dim WithEvents objMailItem As Outlook.MailItem

Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal
ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As
Object, custom() As Variant)
Set oApplication = Application
Set oNameSpace = oApplication.GetNamespace("MAPI")
Set colInsp = oApplication.Inspectors
End Sub
Private Sub colInsp_NewInspector(ByVal Inspector As Inspector)
Dim objItem As Object
Dim objInsp As Inspector
Set objInsp = Inspector
On Error Resume Next
Set objItem = objInsp.CurrentItem
Select Case objItem.Class
Case olMail
Set objMailItem = objItem
End Select
End Sub
Private Sub objMailItem_Open(Cancel As Boolean)
'This is working okay
Debug.Print "Opening"
End Sub
Private Sub objMailItem_Reply(ByVal Response As Object, Cancel As Boolean)
'This line never prints if I click on Reply (to MailItem)
Debug.Print "Reply"
End Sub
'End Code
 
R

Russell Mangel

Are you saying that both of the Events listed are working for you in OL2003?
I am unable to get the Reply event working, the Open event is working.
 
K

Ken Slovak - [MVP - Outlook]

That event works here on all versions of Outlook for me too, including
Outlook 2003.

Is the mail item that is being replied to open at the time and how are you
instantiating the mail item if it isn't opened? In the code you showed you
are instantiating it in NewInspector. So Reply would only fire if the item
was open. If selected in the preview pane and Reply was used then the item
wouldn't have been instantiated as objMailItem and the Reply event would
never fire.
 
R

Russell Mangel

You are correct, the Reply event does fire when the item is open.

This is my problem, I need to catch the reply event when the item is not
open.
e.g. When the user clicks Reply button and it is selected in the preview
pane.
How do I do this?
 
E

Eric Legault [MVP - Outlook]

Below is the code I am using. When I click the Reply button when a message
in Outlook is selected, the myMsg_Open button fires and I have a handle to
the new reply message that has not been sent yet. When I click Send, no
events fire, which is as it should be. The myMsg_Reply only fires when you
have an existing email open and click Reply there. Opening existing emails
of course fires the _Open event.

Keep in mind the Reply event is only in the context of an OPEN message. If
you want to trap a user clicking the Reply button in the main Outlook
window, you'd have to trap the Click event of that toolbarbutton: Dim
WithEvents objReplyButton As Office.CommandBarButton, Set objReplyButton =
ActiveExplorer.CommandBars.FindControl(, 354). But the message opens AFTER
the event, so you can't get a handle to it from Click.

Option Explicit
Dim WithEvents myOlInspectors As Outlook.Inspectors
Dim WithEvents myMsg As Outlook.MailItem

Private Sub myMsg_Open(Cancel As Boolean)

End Sub

Private Sub myMsg_Reply(ByVal Response As Object, Cancel As Boolean)
MsgBox "Hello world!"
End Sub

Private Sub myOlInspectors_NewInspector(ByVal Inspector As Inspector)
If Inspector.CurrentItem.Class <> olMail Then Exit Sub
Set myMsg = Inspector.CurrentItem
End Sub

Private Sub Application_Startup()
Set myOlInspectors = Application.Inspectors
End Sub

Private Sub Application_Quit()
Set myOlInspectors = Nothing
Set myMsg = Nothing
End Sub
 
K

Ken Slovak - [MVP - Outlook]

To add to what Eric said, another method of trapping Reply for a mail item
that isn't opened is to instantiate a MailItem declared WithEvents in a
SelectionChange event handler for the current Explorer. If you want to
handle more than one item then you can set up a Selection wrapper class and
collection, similar to the Explorer and Inspector wrappers that have been
posted so many times here.

Any time you want to handle an indeterminate number of possible open objects
you can wrap them in class modules and stuff them into a collection. As long
as each member class has a WithEvents declaration for a MailItem and is
instantiated in SelectionChange and removed if not currently in the
selection in that event you can do precisely that.

So in that circumstance you would not need to trap the Click event of the
toolbar/menu Reply button, which wouldn't work in any case if the user user
a keyboard shortcut to reply to an item in the Explorer or if they used the
right-click context menu. The wrapper solution handles all those cases.
 
R

Russell Mangel

Thanks, this explains why I was unable to catch the event when the Outlook
Item is not open.
Now, I have to find some code that wraps all this up... I am a very poor at
VB6, (I use C++/C#).
Do you have VB6 sample (class module, or what ever it's called in VB6)?

Thanks
Russell Mangel
Las Vegas, NV

Ken Slovak - said:
To add to what Eric said, another method of trapping Reply for a mail item
that isn't opened is to instantiate a MailItem declared WithEvents in a
SelectionChange event handler for the current Explorer. If you want to
handle more than one item then you can set up a Selection wrapper class and
collection, similar to the Explorer and Inspector wrappers that have been
posted so many times here.

Any time you want to handle an indeterminate number of possible open objects
you can wrap them in class modules and stuff them into a collection. As long
as each member class has a WithEvents declaration for a MailItem and is
instantiated in SelectionChange and removed if not currently in the
selection in that event you can do precisely that.

So in that circumstance you would not need to trap the Click event of the
toolbar/menu Reply button, which wouldn't work in any case if the user user
a keyboard shortcut to reply to an item in the Explorer or if they used the
right-click context menu. The wrapper solution handles all those cases.
 
K

Ken Slovak - [MVP - Outlook]

Well, for an Explorer wrapper class and associated code to implement it in
VB 6 see the ItemsCB sample addin on the Resources page at www.microeye.com
That not only has a wrapper but also has many workarounds for common Outlook
COM addin bugs and limitations.

If you took the Explorer wrapper and added code to the SelectionChange event
handler for the Explorer in the wrapper class you could instantiate a mail
item that is Selection.Item(1). You would just place a Private WithEvents
oMail As Outlook.MailItem in the module declarations section of the class
and then in the init code and in Selection change use: Set oMail =
m_objExpl.Selection.Item(1) to instantiate the change in what's selected.
Then you would have event handlers in the wrapper class for whatever mail
item events you wanted to handle, such as Reply.

The principle would be the same for a Selection collection wrapper as with
the Explorer wrapper (or Items or buttons or whatever other collection you
wanted to wrap). Inside the Explorer wrapper you would handle
SelectionChange and in that event handler:
1. Clear the collection first so no previously selected items are in the
collection. Easy to do using New clsSelectionWrapper.
2. Get each item selected in a loop.
3. For each item add it to a collection using a unique Key value to maintain
a unique collection. I would probably use item.EntryID as my Key value.
The class that was added to the collection would include a WithEvents
declaration for a MailItem so its events could be handled.

Of course that's just an outline, and it will mean more to you once you've
studied ItemsCB. I've also posted an Inspector wrapper many times in these
groups, so you can google for that too.

What I would also do is add some sanity checking like not instantiating the
Selection class if the Explorer.CurrentFolder wasn't an email folder,
checking each selection item to make sure it's a mail item (Inbox can have
other item types come into it like task and meeting requests) and so on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top