Link to home
Start Free TrialLog in
Avatar of jaypappas
jaypappas

asked on

how to determine the name of the toolbar button that was clicked

Function AddToolbarButton(Caption As String, _
                       toolTip As String, macroName As String, _
                       Optional toolbarName As String = "Standard", _
                       Optional FaceID As Long = 325)
Dim objBar As Office.CommandBar
Dim objButton As Office.CommandBarButton

  Set objBar = ActiveExplorer.CommandBars(toolbarName)
  Set objButton = objBar.Controls.Add(msoControlButton)

'objBar.ButtonClick


  With objButton
    .Caption = Caption
    .OnAction = macroName
    .TooltipText = toolTip
    .FaceID = FaceID
    .Style = msoButtonIconAndCaption
    .BeginGroup = True
  End With
End Function


Sub maketoolbarbutton()
     
  Call AddToolbarButton("My button", "Click here", "callmacro")
Call AddToolbarButton("My button 2", "Click here", "callmacro")

    

End Sub


[b]'here is I want callmacro to determine which button was clicked [/b]

Sub callmacro

    'if command button name = some name then
    'do something here
    'end if

end sub

Open in new window

pic-of-buttons.docx
Avatar of puppydogbuddy
puppydogbuddy

to determine the name of the button, try this:
With objButton
    .Name = .ActiveControl.Name
    .Caption = Caption
    .OnAction = macroName
    .TooltipText = toolTip
    .FaceID = FaceID
    .Style = msoButtonIconAndCaption
    .BeginGroup = True
  End With

Avatar of Chris Bottomley
What version?
Assuming its 2007 or later then you could use the caption ... the name itself isn't exposed as easily ... if at all.

Application.ActiveExplorer.CommandBars.ActionControl.Caption

End Sub

Sub callmacro()

    'if Application.ActiveExplorer.CommandBars.ActionControl.Caption = some name then
    'do something here
    'end if

End Sub

Open in new window

It is also possible that you need more than one caption to be the same ... for example againt multiple menus.  If so then of course the caption may not itemise the button but in this case you can add a parameter to the button press:

The modified code below tests the parameter rather than simply returning the caption.  Th eparameter is a new optional.

Chris
Function AddToolbarButton(Caption As String, _
                       toolTip As String, macroName As String, _
                       Optional toolbarName As String = "Standard", _
                       Optional FaceID As Long = 325, _
                       Optional param As Variant)
Dim objBar As Office.CommandBar
Dim objButton As Office.CommandBarButton

  Set objBar = ActiveExplorer.CommandBars(toolbarName)
  Set objButton = objBar.Controls.Add(msoControlButton)

'objBar.ButtonClick


  With objButton
    .Caption = Caption
    .OnAction = macroName
    .TooltipText = toolTip
    .FaceID = FaceID
    .Style = msoButtonIconAndCaption
    .Parameter = param
    .BeginGroup = True
  End With
End Function


Sub maketoolbarbutton()
     
    Call AddToolbarButton("My button", "Click here", "callmacro", , , "Fred Button")
    Call AddToolbarButton("My button 2", "Click here", "callmacro", , , "Doris Button")
    Call AddToolbarButton("My button", "Click here", "callmacro", , , "Fred's Spare Button")
    

End Sub


'[b] 'here is I want callmacro to determine which button was clicked [/b]

Sub callmacro()

    If Application.ActiveExplorer.CommandBars.ActionControl.Parameter = "Fred Button" Then
        MsgBox "Frederick"
    ElseIf Application.ActiveExplorer.CommandBars.ActionControl.Parameter = "Doris Button" Then
        MsgBox "Dorothy"
    ElseIf Application.ActiveExplorer.CommandBars.ActionControl.Parameter = "Fred's Spare Button" Then
        MsgBox "Freddie"
    End If

End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jaypappas

ASKER

Great help