View Full Version : How do i add the custom toolbars programmatically
gopi09_m
05-13-2008, 09:39 PM
Hi,
I wanted to add a custom toolbar "CustomTlbr1" to "View menu-> Toolbars->CustomTlbr1".And also i want to add two custom buttons that will run macro when i click on those custom buttons lets say "Btn1" and "Btn2".
I want this custom toolbar to be applicable to only one excel file called CustomToolbars.xls .Is that possible?
Thanks and Regards
Krrishna
Bob Phillips
05-14-2008, 12:42 AM
Here's an example
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.CommandBars("myToolbar").Delete
On Error GoTo 0
End Sub
Private Sub Workbook_Open()
Dim oCB As CommandBar
Dim oCtl As CommandBarControl
On Error Resume Next
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"
.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 = msoBarTop
End With
End Sub
To add this, go to the VB IDE (ALT-F11 from Excel), and in
the explorer pane, select your workbook. Then select the
ThisWorkbook object (it's in Microsoft Excel Objects which
might need expanding). Double-click the ThisWorkbook and
a code window will open up. Copy this code into there,
changing the caption and action to suit.
This is part of the workbook, and will only exist with the
workbook, but will be available to anyone who opens the
workbook.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.