Item Write event is not always catched

C

Carsten Gehling

I have made an Addin for Outlook 2003 using VSTO. I want it to handle
changes to Appointment Items in the users calendar.

But I have a problem regarding handling the item-events. If you look
at the code below, I try to handle the Item's Write event, put up a
message and "cancel" the event to make sure, that the item is NOT
saved.

My problem lies in that the event-handler is not called on all save
events. The very first time I open an appointment item, changes e.g.
the Subject and press "Save" - my event-handler is called.

But on all subsequent saves, it is not called (even if I change some
of the properties)

This is my code - quite simple:


------------------------------------------------------------------------------
Private Sub ThisApplication_Startup(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Startup
Try
' Handle when an inspector window is opened with e.g. an appointment
item
selectInspectors = Me.Inspectors()
AddHandler selectInspectors.NewInspector, AddressOf
Me.NewInspector_Event
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub


Private Sub NewInspector_Event(ByVal new_Inspector As
Outlook.Inspector)
Dim appItem As Outlook.AppointmentItem
appItem = new_Inspector.CurrentItem

' Handle if items are deleted
AddHandler appItem.Write, AddressOf Me.Item_Write_Event
End Sub


Private Sub Item_Write_Event(ByRef Cancel As Boolean)
MsgBox("Don't do it")
Cancel = True
End Sub
 
K

Ken Slovak - [MVP - Outlook]

Your event handler along with the reference it relies on are being garbage
collected and therefore no longer are valid. Declare appItem at the class
level so it stays alive and doesn't get garbage collected. I'd advise the
same thing for your Inspectors collection so your NewInspector event handler
stays alive.
 

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