Command Bars

J

jfcby

With the macro below is there a way to add a custom button to the
Toolbar without deleing then recreating it?

Sub CreateOtherCommandBar_Test1()
On Error Resume Next
Application.CommandBars(TBN).Delete
On Error GoTo 0
With Application.CommandBars.Add
.Name = TBN
.Visible = True
With .Controls.Add(Type:=msoControlPopup, Before:=1)
.Caption = "Menu 1"
With .Controls.Add(Type:=msoControlButton, Before:=1)
.Caption = "Btn 1"
End With
With .Controls.Add(Type:=msoControlButton, Before:=2)
.Caption = "Btn 2"
End With
End With
End With
End Sub

Thank you for your help,
jfcby
 
J

jfcby

jfcby,

Question:
With the macro above is there a way to add a custom button to the
Toolbar without deleing then recreating it?

Answer:

Yes, with the macro below.

<MACRO CODE>

Sub CreateOtherCommandBar_AddToolBarControls()
'Add toolbar controls
Dim Cmdb
Set Cmdb = CommandBars(TBN).FindControl(Type:=msoControlPopup,
ID:=1)

With Cmdb.Controls.Add(Type:=msoControlButton)
.Caption = "Btn 4"
End With
End Sub

<MACRO CODE>

Thank you for your help and response,
jfcby
 
J

jfcby

A better solution to add custom buttons to the toolbar:

<MACRO CODE>

Sub CreateOtherCommandBar_AddToolBarControlsEx2()
'Add toolbar controls
Dim ctrl
On Error Resume Next
Set ctrl = CommandBars(TBN).Controls("Sort Options") _
'.Controls("My Personal Tools ")
On Error GoTo 0
If ctrl Is Nothing Then
MsgBox "Control does not exist"
Else
With ctrl.Controls.Add(Type:=msoControlButton)
.Caption = "Btn 4"
End With
End If

End Sub

<MACRO CODE>
 
D

Dave Peterson

Check your other post.
With the macro below is there a way to add a custom button to the
Toolbar without deleing then recreating it?

Sub CreateOtherCommandBar_Test1()
On Error Resume Next
Application.CommandBars(TBN).Delete
On Error GoTo 0
With Application.CommandBars.Add
.Name = TBN
.Visible = True
With .Controls.Add(Type:=msoControlPopup, Before:=1)
.Caption = "Menu 1"
With .Controls.Add(Type:=msoControlButton, Before:=1)
.Caption = "Btn 1"
End With
With .Controls.Add(Type:=msoControlButton, Before:=2)
.Caption = "Btn 2"
End With
End With
End With
End Sub

Thank you for your help,
jfcby
 

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