I need to add a delete a button with associated vba code to a toolbar in
Project. I have an example from Excel that does not seem to work. Does
anyone have a Project example?
HarveyH,
Here is sample code from Rod Gill's Book (I suggest you get it if you
are going to do much in VBA)
One sample is for a menu, the other for a toolbar.
-------------------------------------------
Sub AddMenu()
Dim MyMenu As CommandBarControl
Dim MyButton As CommandBarButton
On Error Resume Next
Set MyMenu = CommandBars("Menu Bar") _
.Controls("My Menu")
If MyMenu Is Nothing Then
Set MyMenu = CommandBars("Menu Bar") _
.Controls.Add(Type:=msoControlPopup, _
ID:=1, Before:=9, Temporary:=True)
MyMenu.Caption = "My Menu"
Set MyButton = MyMenu.Controls.Add( _
Type:=msoControlButton, ID:=1, Before:=1)
With MyButton
.OnAction = "AddToolbar"
.Style = msoButtonCaption
.Caption = "Add Toolbar"
End With
Set MyButton = MyMenu.Controls.Add( _
Type:=msoControlButton, ID:=1, Before:=2)
With MyButton
.OnAction = "DeleteBar"
.Style = msoButtonCaption
.Caption = "Delete Toolbar"
End With
Set MyButton = MyMenu.Controls.Add( _
Type:=msoControlButton, ID:=1, Before:=3)
With MyButton
.OnAction = "DeleteMenu"
.Style = msoButtonCaption
.Caption = "Delete Menu"
End With
End If
End Sub
Sub DeleteMenu()
On Error Resume Next
CommandBars("Menu Bar").Controls("My Menu").Delete
End Sub
Sub AddToolbar()
Dim MyBar As CommandBar
Dim MyButton As CommandBarButton
On Error Resume Next
Set MyBar = CommandBars("My Bar")
If MyBar Is Nothing Then
Set MyBar = CommandBars.Add(Name:="My Bar",
Position:=msoBarFloating, Temporary:=True)
MyBar.Visible = True
End If
Set MyButton = MyBar.Controls("MyButton")
If MyButton Is Nothing Then
Set MyButton = MyBar.Controls.Add(Type:=msoControlButton)
With MyButton
.Style = msoButtonCaption
.Caption = "My Macro"
.OnAction = "DeleteBar"
End With
End If
End Sub
Sub DeleteBar()
CommandBars("My Bar").Delete
End Sub
meg99