Hi Oliver,
Thank you for posting in MSDN managed newsgroup!
I have written two sample vba codes for you below which is used to insert one temporary button into the Tools menu.
1: button in menu
'Code "button in Menu" begin---------------------------------------------------
'insert one button into "Tools" menu
Sub InsertOneButtonInToolsMenu()
'You can specify the menu name to insert the button as your wish
'set temporary to true means the button will be automatically deleted
'when Excel is closed
Set objcontrol = Application.CommandBars("Tools").Controls.Add( _
Type:=Office.MsoControlType.msoControlButton, Temporary:=True)
'set the button's name
objcontrol.Caption = "Test button"
'set the click event handler function to the button. The function name can be
'specifeid as you wish. You can define the event handler function in the VBA
'project module so that Excel can locate the function.
objcontrol.OnAction = "Action"
End Sub
'Delete the button from "Tools" button
Sub deletebutton()
Dim obj As Office.CommandBarControl
For Each obj In Application.CommandBars("Tools").Controls
If obj.Caption = "Test button" Then
obj.delete
End If
Next
End Sub
'Define the 'Action' function in one module for your excel workbook
'so that Excel can locate it when you click the button
sub Action()
'write the event handler code as you wish
'this is for test usage
msgbox "click test button"
end sub
'Code end-----------------------------------------------------
2: button in toolbar
'Another sample begin------------------------------------------
sub insertToolbarButton()
Dim taskBar as CommandBar
Dim objButton as CommandBarControl
Set TaskBar = Application.CommandBars.Add("test")
With TaskBar
.Visible = True
.Position = msoBarTop
End With
'2950 is built-in icon for drawing. This is just for illustration. You can change that
'according to your scenario
Set objButton = Application.CommandBars("test").Controls.Add( _
Type:=msoControlButton, ID:=200, Before:=1, Temporary:=True)
With objButton
.Caption = "New Button"
'as the above sample, create the Action function in the workbook module
.OnAction = "Action"
End With
End Sub
sub deleteTaskBarbutton()
'specify the "test" commandbar name to delete
Application.CommandBars("test").Delete
End sub
'Code end-----------------------------------------------------
Please feel free to let me know if you have any further questions.
Does this answer your question? Thank you for using Microsoft NewsGroup!
Wei-Dong Xu
Microsoft Product Support Services
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.