Adding Buttons to Toolbar

H

HarveyH

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?
 
M

meg99

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
 
J

JulieS

Hello Harvey,

What specifically do you wish to delete? You may not need to write the
VBA code if you simply wish to delete a task or resource.

Go to Tools > Customize > Toolbars.
Click the Commands tab.
Click Edit in the Categories list
Scroll the Commands list to find the choice [Delete
Task/Resource/Column]
Click and drag that choice to the toolbar of your liking.
Click the Close button to close the Customize Dialog box.

You now have a button which will delete the selected task, resource or
column (if a column is selected.)

I hope this helps. Let us know how you get along.

Julie
Project MVP

Visit http://project.mvps.org/ for the FAQs and additional information
about Microsoft Project
 

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