Toolbar Issue

M

MichaelA

Hello All,

I have been working my way through Rod's book and have used the VBA
code to add a toolbar with a few buttons. It has worked well. Today,
I added a new button to the existing toolbar module. (In the code
below, the problem occurs with the third button.) Now the same button
is added everytime I open a mpp file leaving me with multiple buttons
for the same macro.

It doesn't happen for the other buttons, though - only the new one.

Any ideas?


thanks
Michael A.


This is my toolbar code:
Sub AddToolbar()
Dim MyBar As CommandBar
Dim MyButton As CommandBarButton


On Error Resume Next
Set MyBar = CommandBars("A-L Macros")
If MyBar Is Nothing Then
Set MyBar = CommandBars.Add(Name:="A-L Macros",
Position:=msoBarTop, Temporary:=True)
MyBar.Visible = True
End If

Set MyButton = MyBar.Controls("Excel Export")
If MyButton Is Nothing Then
Set MyButton = MyBar.Controls.Add(Type:=msoControlButton)
With MyButton
.Style = msoButtonCaption
.Caption = "Excel Export"
.OnAction = "ExtractDataToExcel"
End With
End If

Set MyButton = Nothing
Set MyButton = MyBar.Controls("S-Curve")
If MyButton Is Nothing Then
Set MyButton = MyBar.Controls.Add(Type:=msoControlButton)
With MyButton
.Style = msoButtonCaption
.Caption = "S-Curve"
.OnAction = "exportScurveData"
.DescriptionText = "S-Curve generated in Excel"
End With
End If

Set MyButton = Nothing
Set MyButton = MyBar.Controls("Tasks Drivers")
If MyButton Is Nothing Then
Set MyButton = MyBar.Controls.Add(Type:=msoControlButton)
With MyButton
.Style = msoButtonCaption
.Caption = "Drivers"
.OnAction = "TaskDrivers"
End With
End If
End Sub
 
H

Hans

That's because your Caption is wrong. Caption and button name must match.
Change it to


With MyButton .Style = msoButtonCaption .Caption = "Tasks Drivers" .OnAction = "TaskDrivers" End With


and it should work.

I hope this helps,
Hans



Projectopolis



MichaelA wrote:

Hello All, I have been working my way through Rod's book and have used the VBA code to add a toolbar with a few buttons. It has worked well. Today, I added a new button to the existing toolbar module. (In the code below, the problem occurs with the third button.) Now the same button is added everytime I open a mpp file leaving me with multiple buttons for the same macro. It doesn't happen for the other buttons, though - only the new one. Any ideas? thanks Michael A. This is my toolbar code: Sub AddToolbar() Dim MyBar As CommandBar Dim MyButton As CommandBarButton On Error Resume Next Set MyBar = CommandBars("A-L Macros") If MyBar Is Nothing Then Set MyBar = CommandBars.Add(Name:="A-L Macros", Position:=msoBarTop, Temporary:=True) MyBar.Visible = True End If Set MyButton = MyBar.Controls("Excel Export") If MyButton Is Nothing Then Set MyButton = MyBar.Controls.Add(Type:=msoControlButton) With MyButton .Style = msoButtonCaption .Caption = "Excel Export" .OnAction = "ExtractDataToExcel" End With End If Set MyButton = Nothing Set MyButton = MyBar.Controls("S-Curve") If MyButton Is Nothing Then Set MyButton = MyBar.Controls.Add(Type:=msoControlButton) With MyButton .Style = msoButtonCaption .Caption = "S-Curve" .OnAction = "exportScurveData" .DescriptionText = "S-Curve generated in Excel" End With End If Set MyButton = Nothing Set MyButton = MyBar.Controls("Tasks Drivers") If MyButton Is Nothing Then Set MyButton = MyBar.Controls.Add(Type:=msoControlButton) With MyButton .Style = msoButtonCaption .Caption = "Drivers" .OnAction = "TaskDrivers" End With End If End Sub
 
M

MichaelA

That's because your Caption is wrong. Caption and button name must match.
Change it toWith MyButton .Style = msoButtonCaption .Caption = "TasksDrivers" .OnAction = "TaskDrivers" End With
and it should work.
I hope this helps,

Ah, thank you very much. That is a little trick for new players.
 
M

MichaelA

It appears this toolbar issue has not resolved itself completely.

I have created a template that has the toolbar setup in the
"thisproject" project_open module, such that whenever the template is
opened the required toolbar is loaded. The "ThisProject" in the
Global.MPT has nothing at all.

In the project_open module the following appears (just about straight
out of Rod's book) to create the toolbar:

Set MyBar = CommandBars("My Macros")
If MyBar Is Nothing Then
Set MyBar = CommandBars.Add(Name:="My Macros",
Position:=msoBarTop, temporary:=True)
MyBar.Protection = msoBarNoCustomize
MyBar.Visible = True
End If


All the buttons are coded similarly to:

Dim myBar as CommandBar
Set MyButton = MyBar.Controls("Do Stuff")
If MyButton Is Nothing Then
Set MyButton = MyBar.Controls.Add(Type:=msoControlButton)
With MyButton
.Style = msoButtonIconAndCaption
.FaceId = 142
.Caption = "Do Stuff"
End With
End If

With "On Error" turned off the following lines gives an error "Invalid
procedure call or argument":

Set MyBar = CommandBars("My Macros")

Set MyButton = MyBar.Controls("Do Stuff")

On Error Resume Next bypasses this problem of course. Does anyone
know what is wrong with these lines?


Thanks and regards
Michael A.
 

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