Solved

vba Office Ribbon many buttons, 1 macro...

Posted on 2013-02-06
8
769 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
  • 6
8 Comments
 
LVL 26

Expert Comment

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

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 48

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
 
LVL 14

Author Comment

by:DrTribos
ID: 38863066
Hi Rgonzo1971

Thank you - I will have a look and post back.  Cheers,
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 14

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 14

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 14

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 14

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now