Solved

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

Posted on 2011-02-18
6
334 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:jaypappas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 34931513
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

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34931756
What version?
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34931781
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

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34931950
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

0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 34932913
BTW

If the parameter is not there then it's not so good!  The following fix uses the caption if nd only if the parameter is not passed and is demoed with the adaptation below.

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 String)
Dim objBar As Office.CommandBar
Dim objButton As Office.CommandBarButton

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

'objBar.ButtonClick


    If param = "" Then strParam = Caption
  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")
    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"
    ElseIf Application.ActiveExplorer.CommandBars.ActionControl.Parameter = "My button 2" Then
        MsgBox "No parameter ... but as planned!"
    Else
        MsgBox "Not Freddie, nor Doris nor Fred"
    End If

End Sub

Open in new window

0
 

Author Closing Comment

by:jaypappas
ID: 34951968
Great help
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help to fix the below error for MS Exchange server 2010 I. Out Of office not working II. Certificate error "name on the security certificate is invalid or does not match the name of the site" III. Make Internal URLs and External…
If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question