CommandBarButton Click event problem in Outlook 2003 VB.NET COM add-in

M

Mike Hunsicker

Hi,

I'm running Windows XP Pro SP 1, Outlook 2003 SP 1, Visual Studio.NET 2003
EA. I developed a VB.NET COM add-in for Outlook 2003 which creates a
CommandBar, adds a couple default buttons to the CommandBar, and allows
users to add buttons to the CommandBar at runtime. I referenced the code in
KB302896 while creating this add-in. One of the default buttons (named
"Edit") opens a winform which allows the user to select categories from the
master category list in order to create associated buttons on the custom
CommandBar. The problem I'm having is that the click event fails to fire
for the buttons created at runtime after the winform is launched from the
default "Edit" button because they lose scope. Actually, this isn't quite
true - the last button created at runtime continues to work, which makes
sense I guess because all the buttons created at runtime use the same class
level declaration ("aButton" - see below).

It is a requirement of the program that users be able to create these
buttons at runtime, therefore I don't know how many buttons they require so
I cannot declare them all at the class level individually at design time.
As such, I declare one default button at the class level and use it to build
all buttons created by the user at runtime. I would be very appreciative if
someone could point me in a direction that would help me achieve the needed
functionality. Please see some sample code below.
Thanks,

Mike
______________________________________

' ** Class level declarations. **
Private aOutlookApp As OL.Application
Private aCOMInstance As Object
Private aExplorer As OL.Explorer
Dim aCommandBar As CommandBar
Dim aCommandBars As CommandBars
Dim aCategoryBar As CommandBar
Dim sCategoryBar As String = "Category Manager"
Dim aCMDEdit As CommandBarButton
Dim aCMDClear As CommandBarButton
Dim aButton As CommandBarButton

' ** Code snippet that creates runtime buttons. This is called from the
OnStartupComplete event. **
' Create category buttons
Dim iCount As Integer = 0
Dim bBeginGroup As Boolean = False
Dim aCategories As CategoryCollection = Me.GetCategories() ' Gets categories
from a persisted file used to save user settings

If Not aCategories Is Nothing Then
For Each aCategory As Category In aCategories
iCount += 1
If iCount = 1 Then
bBeginGroup = True
Else
bBeginGroup = False
End If

' Create button
aButton =
aCategoryBar.Controls.Add(MSO.MsoControlType.msoControlButton)
With aButton
.BeginGroup = bBeginGroup
.Caption = aCategory.Name
.Style = MsoButtonStyle.msoButtonCaption
.Tag = aCategory.Name
.OnAction = "!<CategoryToolbar.Connect>"
.Visible = True
End With

AddHandler aButton.Click, AddressOf Me.CategoryButton_Click

Next
End If
 
H

Helmut Obertanner

Hello Mike,

only my Idea:
hold a reference to your dynamically created buttons in a collection.

--
Freundliche Grüße / with regards

Helmut Obertanner
Technical Consultant

Softwaredevelopment
DATALOG Software AG | Zschokkestr. 36 | D-80687 Munich
web: www.datalog.de


.... and IT works!
 
M

Mike Hunsicker

Thanks Helmut. That worked perfectly!

Helmut Obertanner said:
Hello Mike,

only my Idea:
hold a reference to your dynamically created buttons in a collection.

--
Freundliche Grüße / with regards

Helmut Obertanner
Technical Consultant

Softwaredevelopment
DATALOG Software AG | Zschokkestr. 36 | D-80687 Munich
web: www.datalog.de


... and IT works!
 

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