Here is some VB.net code to see what I mean.
imports Extensibility
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop.Word
#Region " Read me for Add-in installation and setup information. "
' When run, the Add-in wizard prepared the registry for the Add-in.
' At a later time, if the Add-in becomes unavailable for reasons such as:
' 1) You moved this project to a computer other than which is was originally
created on.
' 2) You chose 'Yes' when presented with a message asking if you wish to
remove the Add-in.
' 3) Registry corruption.
' you will need to re-register the Add-in by building the $SAFEOBJNAME$Setup
project,
' right click the project in the Solution Explorer, then choose install.
#End Region
<GuidAttribute("992AA296-EBEE-38EE-BB6E-8F8373A53182"),
ProgIdAttribute("TemplateMenu.Connect")> Public Class Connect
Implements Extensibility.IDTExtensibility2
Private MainMenuBar As CommandBar
Private MenuBarItem As CommandBarControl
Private WithEvents MenuItem As CommandBarButton
Dim applicationObject As Object
Dim addInInstance As Object
Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnBeginShutdown
End Sub
Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnDisconnection(ByVal RemoveMode As
Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnDisconnection
End Sub
Public Sub OnConnection(ByVal application As Object, ByVal connectMode As
Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As
System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
applicationObject = application
addInInstance = addInInst
End Sub
Public Sub OnStartupComplete(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnStartupComplete
test()
End Sub
Private Sub InitMenuBarItems(ByVal Caption As String)
Try
Me.MainMenuBar = applicationObject.CommandBars.add("Worksheet Menu Bar")
Me.MenuBarItem = Me.MainMenuBar.Controls.Add(MsoControlType.msoControlPopup,
Temporary:=True)
Dim cbc As CommandBarControl
cbc = DirectCast(Me.MenuBarItem, CommandBarControl)
cbc.Caption = Caption
cbc.Visible = True
Catch ex As Exception
MsgBox(ex.Message, , ex.Source)
End Try
End Sub
Private Function CreateButton( _
ByVal Parent As CommandBarPopup, _
ByVal Caption As String) As CommandBarButton
Dim cbc As CommandBarControl
cbc = Nothing
Try
cbc = Parent.Controls.Add(MsoControlType.msoControlButton, Temporary:=True)
cbc.Caption = Caption
cbc.Visible = True
Catch ex As Exception
MsgBox(ex.Message, , ex.Source)
End Try
Return DirectCast(cbc, CommandBarButton)
End Function
Private Sub MenuItem_Click( _
ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, _
ByRef CancelDefault As Boolean) Handles MenuItem.Click
'Here I want the click event to trigger on all 3 menuitems I add in sub
Test, but it only triggers at the last added menuitem
MsgBox(Ctrl.Tag)
End Sub
Sub test()
InitMenuBarItems("&Travel Tools")
Dim i As Integer
For i = 1 To 3
Me.MenuItem = Me.CreateButton( _
DirectCast(Me.MenuBarItem, CommandBarPopup), "Alt " & i.ToString)
'In the tag property I put the variable information
Me.MenuItem.Tag = i.ToString
Next i
End Sub
End Class