M
Mo
Hi all,
(I have posted this earlier in microsoft.public.dotnet.framework.interop but
I think I'll have better luck here amongst all Word experts )
I'm trying to create an office menu dynamically by creating an shared add-in
in vb.net. The add-in is supposed to create the menu at start up in i.e.
Microsoft Word. Each menu item should have an event tied to it. The thing is
that the
menu doesn't have a fixed number of items - it varies over time. Each time
you start Word it creates the menu.
My problem is how to dynamically create the event handling for the different
menu items? I have created one event handler to which I pass the choosen
menuitem. But it is only the last added menuitem thet gets handled by the
event handler?
In Word you would use the OnAction-property which runs a desired macro. But
I don't want to have a macro in Word as the whole idea is to create this
functionality through an add-in.
Can this be done in some way? Any ideas are highly appreciated.
TIA,
Peeter
Below is some VB.net code to test what I mean.
It as an shared add-in project.
'*******************************************************************
'*******************************************************************
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
'In Word VBA you used the OnAction property instead of the click_event to
tell which macro to run when a menuitem was clicked
'Me.MenuItem.OnAction="MyCodeToRun"
Next i
End Sub
End Class
(I have posted this earlier in microsoft.public.dotnet.framework.interop but
I think I'll have better luck here amongst all Word experts )
I'm trying to create an office menu dynamically by creating an shared add-in
in vb.net. The add-in is supposed to create the menu at start up in i.e.
Microsoft Word. Each menu item should have an event tied to it. The thing is
that the
menu doesn't have a fixed number of items - it varies over time. Each time
you start Word it creates the menu.
My problem is how to dynamically create the event handling for the different
menu items? I have created one event handler to which I pass the choosen
menuitem. But it is only the last added menuitem thet gets handled by the
event handler?
In Word you would use the OnAction-property which runs a desired macro. But
I don't want to have a macro in Word as the whole idea is to create this
functionality through an add-in.
Can this be done in some way? Any ideas are highly appreciated.
TIA,
Peeter
Below is some VB.net code to test what I mean.
It as an shared add-in project.
'*******************************************************************
'*******************************************************************
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
'In Word VBA you used the OnAction property instead of the click_event to
tell which macro to run when a menuitem was clicked
'Me.MenuItem.OnAction="MyCodeToRun"
Next i
End Sub
End Class