Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vba Office Ribbon many buttons, 1 macro...

Posted on 2013-02-06
8
Medium Priority
?
860 Views
Last Modified: 2013-02-07
Hi All

I'm not even sure how to phrase this question.  What I want to do is have multiple buttons on my ribbon that all fire the same macro, however I want the macro to do different things depending on which button was pressed... is this possible?  What would be the best way to achieve this?

sub AllButtons ()
  Select case ?????
     case "button 1"
     ' do this

     case "button 2"
   ' do that
   ...
End Sub
0
Comment
Question by:DrTribos
[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
  • 6
8 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 38862967
The best way would be to create a separate macro for each button.
0
 
LVL 15

Author Comment

by:DrTribos
ID: 38862971
Noted... still interested to know if it is possible and also why it is (if it turns out to be) bad practice to use 1 macro...

thanks
0
 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 1200 total points
ID: 38863049
Hi,

Please refer to http://msdn.microsoft.com/en-us/library/aa942866(v=vs.80).aspx



Public Sub OnActionCallback(ByVal control As Office.IRibbonControl, _
    ByVal isPressed As Boolean)

    If (control.Id = "checkBox1") Then
        MessageBox.Show("You clicked " + control.Id)
    Else
        MessageBox.Show("You clicked a different control.")
    End If

End Sub

Open in new window


Regards
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 15

Author Comment

by:DrTribos
ID: 38863066
Hi Rgonzo1971

Thank you - I will have a look and post back.  Cheers,
0
 
LVL 15

Author Comment

by:DrTribos
ID: 38866381
Ok... stuck...  I am getting an error message: "wrong number of arguments or invalid property assignment"

This is my ribbon code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="OnLoad">

  <ribbon>
    <tabs>
      <tab id="test" label="test" keytip="T">
        <group id="myID1" label="Test MyID">
          <box id="myID2" boxStyle="vertical">
            <button id="btn1" label="Effect 1" onAction="AllButtons"/>
            <button id="btn2" label="Effect 2" onAction="AllButtons"/>
            <button id="btn3" label="Effect 3" onAction="AllButtons"/>
          </box>
	  </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

Open in new window


And this is my VBA...  BTW... I created a new module and the macros were not found so I moved them and at least they were found; but is there a special place to put this code?

Public Sub OnActionCallback(ByVal control As Office.IRibbonControl, _
    ByVal isPressed As Boolean)

    If (control.ID = "btn1") Then
        MessageBox.Show ("You clicked " + control.ID)
    Else
        MessageBox.Show ("You clicked a different control.")
    End If

End Sub

Sub AllButtons()
       MsgBox "test"
End Sub 

Open in new window


Many thanks :-)
0
 
LVL 15

Author Comment

by:DrTribos
ID: 38866495
Um... feeling kinda silly... changed code to:

Public myRibbon As IRibbonUI

Public Sub OnLoad(ribbon As IRibbonUI)
    Set myRibbon = ribbon
MsgBox "ribbon loaded"
End Sub

Public Sub AllButtons(ByVal control As Office.IRibbonControl, _
    ByVal isPressed As Boolean)

    If (control.ID = "btn1") Then
        MessageBox.Show ("You clicked " + control.ID)
    Else
        MessageBox.Show ("You clicked a different control.")
    End If

End Sub

Open in new window


and now I am getting error / info message "argument not optional"
0
 
LVL 15

Author Comment

by:DrTribos
ID: 38866549
Ooo it's working!

Public myRibbon As IRibbonUI

Public Sub OnLoad(ribbon As IRibbonUI)
    Set myRibbon = ribbon
MsgBox "ribbon loaded"
End Sub

Public Sub AllButtons(control As IRibbonControl)

' Public Sub AllButtons(ByVal control As Office.IRibbonControl, ByVal isPressed As Boolean)
'
    If (control.ID = "btn1") Then
        MsgBox ("You clicked " + control.ID)
       ' returnedVal = 1
    Else
        MsgBox ("You clicked a different control.")
       ' returnedVal = 2
    End If

End Sub

Open in new window

0
 
LVL 15

Author Closing Comment

by:DrTribos
ID: 38866590
Thanks, your link got me moving (slowly) in the right direction... I have much to learn about ribbon!!

Opps... for anyone watching my post above is incorrect; here is the code that actually worked...

Public Sub AllButtons(control As IRibbonControl)

' Public Sub AllButtons(ByVal control As Office.IRibbonControl, ByVal isPressed As Boolean)
'
    If (control.ID = "btn1") Then
        MsgBox ("You clicked " + control.ID)
       ' returnedVal = 1
    Else
        MsgBox ("You clicked a different control.")
       ' returnedVal = 2
    End If

End Sub

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

721 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