You can use CommandBars and CommandBarControls classes to accomplish your work.
Please note some considerations:
- When adding a control, you should specify the Temporary property as True or False
- If you don't remove your controls before adding more, and they're the same, they will be duplicated. Best way to remove appropriately is when adding buttons, add some unique text to the Tag property (so you don't remove other controls that may be there).
Adding as Temporary (all is happening in the AddIns tab):
Dim cBar as CommanBars
Dim cBtn as CommandBarButton
Set cBar = CommandBars("Worksheet Menu Bar")
Set cBtn = cBar.Controls.Add(Type:=msoControlButton, Temporary:=True)
With cBtn
.Visible = True
.Enabled = True
.Tag = "coolButton"
.FaceId = 123
.Caption = "Cool Trick"
.Style = msoButtonIconAndCaption
.TooptipText = "this button does something magically!!"
.OnAction = "Module1.SomeMacro"
End with
Removing:
Dim cBar as CommanBars
Dim cBtn as CommandBarButton
Set cBar = CommandBars("Worksheet Menu Bar")
For Each cBtn in cBar.Controls
If cBtn.Tag = "coolButton" Then: cBtn.Delete
Next cBtn