Ribbon command button click events

Joseph.Marro

Board Regular
Joined
Nov 24, 2008
Messages
153
Hello,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I am stumped. Is it possible to capture the click event of a command button from the ribbon? I would like to piggy back off the click event to execute my code. The following post deals with the same question:<o:p></o:p>
http://www.mrexcel.com/forum/showthread.php?t=448695&highlight=Increase+Indent+click+event+WithEvent<o:p></o:p>
Unfortunately, I have been unable to make it work in 2007. Like the OP, I would like to capture the click events for the "Increase Indent" and "Decrease Indent" buttons. Any assistance would be greatly appreciated.<o:p></o:p>
<o:p></o:p>
Thank you,<o:p></o:p>
<o:p></o:p>
Joseph Marro
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Any takers? I am only looking for a starting point. I can't even locate the control when browsing through the object model.

Thank you,

Joseph Marro
 
Upvote 0
It is if you add a Command item to ribbonx.

In your code module use callbacks.
Code:
Public Sub rbxIndentDecrease_onAction(control As IRibbonControl, ByRef cancelDefault)
'
' Code for onAction callback. Ribbon control command
'
    MsgBox "Indent De"
    cancelDefault = False
End Sub
Public Sub rbxIndentIncrease_onAction(control As IRibbonControl, ByRef cancelDefault)
'
' Code for onAction callback. Ribbon control command
'
    MsgBox "Indent Increase"
    cancelDefault = False
End Sub

ribbonx would look like this. had to add extra space after lessthan sign to allow posting.
Code:
< customUI  xmlns="http://schemas.microsoft.com/office/2006/01/customui" >
	< commands >
		< command 
			idMso="IndentDecreaseExcel"
			onAction="rbxIndentDecrease_onAction"/>
		< command 
			idMso="IndentIncreaseExcel"
			onAction="rbxIndentIncrease_onAction"/>
	< /commands >
< /customUI >
 
Upvote 0
Andy,

Thank you for the information and code. I will give it a try over the weekend and let you know how it goes. I miss 2003 command bars, the ribbon is so painful to work with.

Thank you,

Joseph Marro
 
Upvote 0
Joseph,
You can add via code the buttons with "OnAction" property the same way as in Excel 2003.
In Excel 2007+ the old-style buttons goes to the Add-Ins tab of the Ribbon
Rich (BB code):

Sub Auto_Open()
  Dim cmd As CommandBarButton
  
  DeleteControls
  Set cmd = Application.CommandBars("Standard").Controls.Add(Type:=msoControlButton, Temporary:=True)
  With cmd
    .Caption = "Indent+"
    .TooltipText = "Click me to increase indent"
    .Style = msoButtonCaption
    .OnAction = "MacroIndentIncrease"
  End With
  
  Set cmd = Application.CommandBars("Standard").Controls.Add(Type:=msoControlButton, Temporary:=True)
  With cmd
    .Caption = "Indent-"
    .TooltipText = "Click me to decrease indent"
    .Style = msoButtonCaption
    .OnAction = "MacroIndentDecrease"
  End With
  
End Sub

Sub MacroIndentIncrease()
  MsgBox "It's MacroIndentIncrease"
End Sub
  
Sub MacroIndentDecrease()
  MsgBox "It's MacroIndentIncrease"
End Sub
  
Private Sub Auto_Close()
  DeleteControls
End Sub

Private Sub DeleteControls()
  On Error Resume Next
  With Application.CommandBars("Standard")
    .Controls("Indent+").Delete
    .Controls("Indent-").Delete
  End With
End Sub
It's possible due to the backward compatibility.

Regards,
 
Last edited:
Upvote 0
ZVI,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Thank you for your post. After taking another look at my approach to implementing this solution I have decided to use your method. Having buttons on the Add-ins tab will be less likely to raise any confusion. Piggy backing off the existing buttons really doesn't let the user know that something special is happening when the button is pushed. I greatly appreciate you assistance.<o:p></o:p>
<o:p></o:p>
Andy,<o:p></o:p>
<o:p></o:p>
Thank you for the solution to my original question. I will defiantly use the approach in other projects. I greatly appreciate you assistance.<o:p></o:p>
<o:p></o:p>
Thank you,<o:p></o:p>
<o:p></o:p>
Joseph Marro<o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top