Event trigers only first two times from FromRegion.

L

Lukasz

Hello,

I have a custom VSTO Addin with FormRegion. In that add_in I wan't to hook to the events triggered when a user presses Reply or Forward buttons of the inspector window running my FormRegion. To do this I do:

Private Sub ChoixProjetRegion_FormRegionShowing(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.FormRegionShowing

If TypeOf (Me.OutlookItem) Is Outlook.MailItem AND Me.OutlookFormRegion.FormRegionMode = Outlook.OlFormRegionMode.olFormRegionRead Then
AddHandler CType(Me.OutlookItem, Outlook.MailItem).Reply, (AddressOf OutlookApplication_ItemReply)

End If

End Sub



Private Sub OutlookApplication_ItemReply(ByVal Item As Object, ByRef Cancel As Boolean)
MsgBox("here")
End Sub

It works. I see My Message Box when I press Reply. The Problem Is that it only works the first two times. ie, on the reply window I close it, and press Reply again on the original window and I get my MsgBox again, followed by the reply window. I close it again, and click on Reply thisr time, but this time the Reply window comes on without the MsgBox.

If I keep closing the Reply window and pressing the Reply button again a few times, eventually I'll get:

"COM object that has been separated from its underlying RCW cannot be used."



Does anyone know what's going on?.
Submitted using http://www.outlookforums.com
 
K

Ken Slovak - [MVP - Outlook]

Your handler is being eaten by the garbage collector. Make sure to declare
all relevant objects at class level, where they won't be garbage collected
and will remain in scope. You need a class level MailItem object that you
assign to that item and then you add the event handler to that class level
object.
 
L

Lukasz

That was it. Thank you Ken. It even took care of the COM error. My woes are not over though. Now the event handler executes between two and four times whenever I click on the Reply. Any idea why?

Thanks,

Lukasz

kenslovak wrote on Fri, 23 April 2010 10:5
 
K

Ken Slovak - [MVP - Outlook]

Are you removing the handler where the item that was showing the form region
is no longer current, and adding it back for any new items? When it fires
more than once is it for the same item each time? Any possibility that
you've added the same handler more than once for the same item?
 
L

Lukasz

You got it again. I added RemoveHandler to the FormRegionClosed event and there's now only one call to my event handler.

Thank you very much.

Lukasz

kenslovak wrote on Fri, 23 April 2010 12:4
 

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