Solved: assign macros to the controls of a toolbar
I made a toolbar using tool/customize. then from commands/Rearrange commands/modify section I assign the macros I defined in a module to the controls. the toolbar works on my pc, but when I try to use the controls in another pc it gives an error that the macro cannot be find. The formula I typed for macro will be changed to sth else and someother path will be added to the formula for example 'F:\...\ .... \name of excel file.xls'! name of the subrutine. the formula should only be 'name of excel file.xls! name of the subrutine.When I remove the extra part for example F:\....\...\ and changed it to name of exelfile.xls! name of subrutine then the controls work.I am confused. How can I get rid of the extra part of the formula(the path) so that I dont need to delete for each control when I run my program on other pcs?
To clarify it, I will use Bob's post ...
Quote:
Originally Posted by xld
This one means that you don't take the time to add a toolbar manually but
use this code to create and when finished, delete the toolbar for the current workbook.
Create the toolbar dynamically
[vba]
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Delete toolbar that you have created
Application.CommandBars("myToolbar").Delete
On Error GoTo 0
End Sub
Private Sub Workbook_Open()
When workbook opens, create toolbar
Dim oCB As CommandBar
Dim oCtl As CommandBarControl
On Error Resume Next
If for some reason the toolbar is still present, delete it, or you will get an error
Application.CommandBars("myToolbar").Delete
On Error GoTo 0
Set oCB = Application.CommandBars.Add(Name:="myToolbar", temporary:=True)
With oCB
Set oCtl = .Controls.Add(Type:=msoControlButton)
With oCtl
.BeginGroup = True
.Caption = "savenv"
The line beneath this sentence means when pushed on button with
caption savenv execute macro savenv
.OnAction = "savenv"
.FaceId = 27
End With
Set oCtl = .Controls.Add(Type:=msoControlButton)
With oCtl
.Caption = "savemyprog"
.OnAction = "savemyprog"
.FaceId = 28
End With
Set oCtl = .Controls.Add(Type:=msoControlButton)
With oCtl
.Caption = "macro4"
.OnAction = "macro4"
.FaceId = 29
End With
Set oCtl = .Controls.Add(Type:=msoControlButton)
With oCtl
.Caption = "dater"
.OnAction = "dater"
.FaceId = 30
End With
.Visible = True
Position of the toolbar
.Position = msoBarTop
End With
End Sub
[/vba]
Charlize
This is what I can explain
Dear All,
I have an excel file. I want to have the toolbar in sheet2. I wrote the macros for example "Private Sub AddStreams" in module1 which is inside the same file. then from tools/customize I try to assign this fomula to my control "filename.xls!AddStreams" but the formula changed to another thing and the path is added to it.
Also In module6 which is inside this file,I creat a toolbar as back up,some part of the codes is:
Sub CreateToolBar()
Application.CommandBars.Add(Name:="Object Palette1(BackUp)").Visible = False
Set Toolbar = Application.CommandBars("Object Palette1(BackUp)")
With Toolbar
.Controls.Add Type:=msoControlButton, ID:=204, Before:=1
.Controls.Add Type:=msoControlButton, ID:=182, Before:=2
End With
ThisWorkbook.Sheets("FrmTask1Buffer").Pictures("Cross").Copy
Set MyButton = Toolbar.Controls.Add(Type:=msoControlButton)
MyButton.PasteFace
Toolbar.Controls(3).Caption = "Cancel Selection"
Toolbar.Controls(3).OnAction = "Breakchoice"
With Toolbar
.Controls.Add Type:=msoControlButton, ID:=2642, Before:=4
.Controls.Add Type:=msoControlButton, ID:=1142, Before:=5
End With
Toolbar.Controls(4).Caption = "Connector"
Toolbar.Controls(4).OnAction = "AddConnector"
Toolbar.Controls(5).Caption = "Material Stream"
Toolbar.Controls(5).OnAction = "AddStreams"
But non of the control of these two toolbars work, because of the path added!!! Is it clesr now or why dont you want to look at my file!!!
Thank you,
Mariam