Solved

vba Office Ribbon many buttons, 1 macro...

Posted on 2013-02-06
8
834 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 400 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
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 …

628 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