Reply/Reply All

G

Guest

VSTO and VB.Net 2005, is there REALLY a way to capture the Reply or Reply All
events? I see lots of discussions about Inspectors, but there's nothing that
actually captures the event. I see lots of code regarding assumptions such as
the size of the e-mail is 0 and the Reciptients are greater than 1 but that
would give me no way to cancel a Reply or Reply All.

The goal is to prompt the user upon a Reply/Reply All and allow the user to
cancel the event. In VB6 this was very possible using Item Events, but that
seems to be gone in VSTO. Any ideas?

Thanks,
DG
 
D

Dmitry Streblechenko

Trap the selection change events (Explorer.SelectionChange). For each item
in the Explorer.Selection collection, trap the Reply/ReplyAll events.
If the user can also reply from an inspector, you need to also trap these
events on all open inspectors.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
G

Guest

Dmitry, as always, thank you for guiding me....

Here's the code I am using. Appears a little bugy in that it only allows it
to capture the Reply All once until the next Explorer.SelectionChange event.
Is there anyway to avoid that that so a user can Reply All/Cancel unlimited
number of times?

Dim WithEvents explorer As Outlook.Explorer = Nothing
Dim selectedItems As New System.Collections.ArrayList()

Private Sub ThisApplication_Startup(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Startup
explorer = Me.Explorers.Application.ActiveExplorer
AddHandler explorer.SelectionChange, AddressOf
explorer_SelectionChange
End Sub

Private Sub explorer_SelectionChange() Handles explorer.SelectionChange
selectedItems.Clear()

For Each selectedItems As Object In explorer.Selection
Dim mailItem As Outlook.MailItem = TryCast(selectedItems,
Outlook.MailItem)

If (mailItem IsNot Nothing) Then
AddHandler mailItem.ReplyAll, AddressOf mailItem_ReplyAll
End If
Next
End Sub

Private Sub mailItem_ReplyAll(ByVal Response As Object, ByRef Cancel As
Boolean)
If MsgBox("Are you sure?", MsgBoxStyle.Question + MsgBoxStyle.YesNo,
"ddd") = MsgBoxResult.No Then
Cancel = True
End If
End Sub
 
D

Dmitry Streblechenko

The line
Dim mailItem As Outlook.MailItem
declares a local variable which will be freed (and hence its events will be
dropped) as soon as it goes out of scope and the GC releases it.
Store all such items in a list rather than keep them as local variables (or,
worse yet, as a *single*local variable).

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
G

Guest

Okay, that worked like a champ. I can't believe I missed that. One last bug.

So in the code I sent last night, the MailItem_ReplyAll will run twice AFTER
every selection change.

If you are looking at your Outlook Inbox. You choose a specific e-mail, you
choose Reply All, and the MailItem_ReplyAll routine runs twice. It's as if
I'm not doing a good garbage collection on something. I thought it was
MailItem but it's not.

Any ideas?
 
G

Guest

Sorry Dmitry, but I cannot figure out the best place to put
RemoveHandler mailItem.ReplyAll, AddressOf mailItem_ReplyAll

At first I added it to the SectionChange routine, but that just killed the
event, so then I thought I had to add it to the ReplyAll event after it runs,
but that did nothing, same error.

Sorry for all your time. I appreciate this.
 
G

Guest

Have a look:
public class ThisApplication
Dim WithEvents explorer As Outlook.Explorer = Nothing
Dim selectedItems As New System.Collections.ArrayList()
Public mailItem As Outlook.MailItem = Nothing

Private Sub ThisApplication_Startup(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Startup

explorer = Me.Explorers.Application.ActiveExplorer
AddHandler explorer.SelectionChange, AddressOf
explorer_SelectionChange
End Sub

Private Sub explorer_SelectionChange() Handles explorer.SelectionChange
selectedItems.Clear()

For Each selectedItems As Object In explorer.Selection
mailItem = TryCast(selectedItems, Outlook.MailItem)
If (mailItem IsNot Nothing) Then
AddHandler mailItem.ReplyAll, AddressOf mailItem_ReplyAll
End If
Next

If (mailItem IsNot Nothing) Then
RemoveHandler mailItem.ReplyAll, AddressOf mailItem_ReplyAll
End If
End Sub

Private Sub mailItem_ReplyAll(ByVal Response As Object, ByRef Cancel As
Boolean)
'blah blah blah
End Sub
End Class
 
D

Dmitry Streblechenko

The RemoveHandler part must come before AddHandler loop, otherwise you will
remove the handler that you just added.
Also, your code will only handle one selected item - instead of using a
single mailitem varisble, use a list.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 

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