Solved

vba Office Ribbon many buttons, 1 macro...

Posted on 2013-02-06
8
790 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 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 49

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

825 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