Usually to handle multiple open Inspectors we use wrapper classes in a
collection to hold the references open so they don't get garbage collected.
The wrapper class has declarations for Inspector and the item shown in the
Inspector. The UI is usually added on the first Inspector.Activate() event.
The reason for adding the Inspector in Activate() is that in NewInspector()
you get a weak object reference in Outlook 2007 that doesn't have all
properties of the item instantiated.
For avoiding multiple clicks you make sure to create a unique Tag property
for each button you create. That way the click only fires in the specific
Inspector where the button was clicked.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
"Steffen Grellmann" wrote in message
...
Hi newsgroup,
I'm trying to add a custom button to a new inspectors commandbar in
Outlook 2003. Word is used as e-mail editor. Everything is working
fine so far.
But what would be the correct technique to add a handler to my custom
button? If I add the handler like shown in the code below, with every
new inspector that is opened a new handler is added to my button. This
lets the click procedure fire that many times inspectors has been
opened so far. Would it be a solution to initialize a Boolean variable
to check if the handler has been added or which is the preferred way
to add the button handler in this constellation?
Any help is highly appreciated.
Kind regards,
Steffen
Public Class ThisAddIn
Dim MyButton As Office.CommandBarButton
Dim selectInspectors As Outlook.Inspectors
Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Startup
selectInspectors = Application.Inspectors()
AddHandler selectInspectors.NewInspector, AddressOf
NewInspector_Event
End Sub
Private Sub NewInspector_Event(ByVal new_Inspector As
Outlook.Inspector)
If
new_Inspector.CurrentItem.MessageClass.ToUpper.Con tains("IPM.NOTE")
Then
Try 'inserting Button
MyButton = new_Inspector.CommandBars.FindControl(, ,
"MyTag")
If MyButton Is Nothing Then 'only if doesn't exist
MyButton =
new_Inspector.CommandBars.Item("E-mail").Controls.Add(1, , , , False)
With MyButton
.Style =
Office.MsoButtonStyle.msoButtonIconAndCaption
.Caption = "MyCaption"
.Tag = "MyTag"
.TooltipText = "MyTooltipp"
.FaceId = 1100
End With
AddHandler MyButton.Click, AddressOf ButtonClick
Else
AddHandler MyButton.Click, AddressOf ButtonClick
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
Private Sub ButtonClick(ByVal ctrl As Office.CommandBarButton,
ByRef Cancel As Boolean)
MessageBox.Show("You clicked: " + ctrl.Caption)
End Sub
End Class