Need help: Commandbar event

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
 
S

Shauna Kelly

Hi Mo

I'm not sure about a VB.net shared add-in, but in other worlds you have to
do the following:

1. Create the button. Either note the ID, or if you don't explicitly choose
an ID it will be 1. All your buttons should have the same ID.

2. Give the button a .Tag which is what you'll use to identify your buttons
from everyone else's. So think of a name that reflects your application. All
your buttons should have the same .Tag.

3. Give each individual button a .Parameter that tells your code what to do
when the user clicks the button. Each button must have a unique .Parameter
because it's what your code uses to distinguish a user clicking one button
from another. In the code below, I've used "MyParameter1" and
"MyParameter2".

4. Create a
Private mbtnMyButton As Office.CommandBarButton
Bear the scope in mind. See
http://blogs.msdn.com/vsto/archive/2004/01/29/64449.aspx.


5. Hook the button using something like
mbtnMyButton = CType(appWord.CommandBars.FindControl(Tag:="MyTag"),
Microsoft.Office.Core.CommandBarButton)
AddHandler MyButton.Click, AddressOf mbtnButtonHandler

The hook will capture all buttons with the same ID/Tag combination. You only
need to hook one button, no matter how many buttons with your combination of
ID and Tag you have on your command bar.


6. The event handler looks something like this:

Private Sub mbtnButtonHandler(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)

If Ctrl.Tag = "MyTag" Then

Select Case Ctrl.Parameter

Case "MyParameter1"
'Call your MyParameter1 code here

Case "MyParameter2"
'Call your MyParameter2 code here

endif



Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
M

Mo

Oh yes! Thank you so much!
Peeter


Shauna Kelly said:
Hi Mo

I'm not sure about a VB.net shared add-in, but in other worlds you have to
do the following:

1. Create the button. Either note the ID, or if you don't explicitly
choose an ID it will be 1. All your buttons should have the same ID.

2. Give the button a .Tag which is what you'll use to identify your
buttons from everyone else's. So think of a name that reflects your
application. All your buttons should have the same .Tag.

3. Give each individual button a .Parameter that tells your code what to
do when the user clicks the button. Each button must have a unique
.Parameter because it's what your code uses to distinguish a user clicking
one button from another. In the code below, I've used "MyParameter1" and
"MyParameter2".

4. Create a
Private mbtnMyButton As Office.CommandBarButton
Bear the scope in mind. See
http://blogs.msdn.com/vsto/archive/2004/01/29/64449.aspx.


5. Hook the button using something like
mbtnMyButton = CType(appWord.CommandBars.FindControl(Tag:="MyTag"),
Microsoft.Office.Core.CommandBarButton)
AddHandler MyButton.Click, AddressOf mbtnButtonHandler

The hook will capture all buttons with the same ID/Tag combination. You
only need to hook one button, no matter how many buttons with your
combination of ID and Tag you have on your command bar.


6. The event handler looks something like this:

Private Sub mbtnButtonHandler(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)

If Ctrl.Tag = "MyTag" Then

Select Case Ctrl.Parameter

Case "MyParameter1"
'Call your MyParameter1 code here

Case "MyParameter2"
'Call your MyParameter2 code here

endif



Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 

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