CommandBarButton with toggle property (up & down)

  • Thread starter Jean-Pierre Bidon
  • Start date
J

Jean-Pierre Bidon

Hi,
I create a new CommandBar, I had a button, more or less like this :

With CommandBars.Add(Name:=stCBarName)
With .Controls.Add(Type:=msoControlButton, _
temporary:=True)
etc.

I would like that the button be a "toggle button" that offers 2 possible
states "up" and "down".
Is that possible .. and how.
Thank you in advance.

Jean-Pierre Bidon
Interstat
5 place de la République
75003 Paris
Tél: 01 45 49 19 17
 
C

Chip Pearson

Try something like the following code:

Sub AAA()
Dim CmdBar As Office.CommandBar
Dim Ctrl As Office.CommandBarButton
On Error Resume Next
Application.CommandBars("Chip").Delete
On Error GoTo 0
Set CmdBar = Application.CommandBars.Add(Name:="Chip", temporary:=True)
Set Ctrl = CmdBar.Controls.Add(Type:=msoControlButton)
Ctrl.Caption = "click me"
Ctrl.State = msoButtonUp
Ctrl.OnAction = "'" & ThisWorkbook.Name & "'!TheProc"
CmdBar.Visible = True

End Sub

Sub TheProc()
With Application.CommandBars.ActionControl
If .State = msoButtonDown Then
.State = msoButtonUp
Else
.State = msoButtonDown
End If
End With
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top