index > Visual Basic for Applications (VBA) > Creating a Toolbar and a button that is linked to a macro

Creating a Toolbar and a button that is linked to a macro


Hello,

I'm looking to add toolbar and then a button inside that toolbar that will be linked to a script using vba. Baiscally, on excel start a toolbar would be placed at the end of the existing ones at the top that would have a button named Run and would be linked to a macro named play. On close I want that to be deleted.

polska2180

Hi

I think this is nearly what you want. Create a workbook and put the first part of the code in ThisWorkbook module, and the second part in a standard code module.

Save the working program in the folder set in Excel as the one where all files will be opened at Startup.

This is actually a cut down version of some code I use, so I've left some extras in that save the position of the command bar in the Registry and use this information when the workbook is opened.

Good luck

Peter Mo.

'====================================================================
'
' ThisWorkbook module
'
'====================================================================
Option Explicit

Dim myCbr As CommandBar

'--------------------------------------------------------------------
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' tidy everything up

' save position on command bar

SaveSetting strAppName, strStartup, strCBPosn, myCbr.Position
SaveSetting strAppName, strStartup, strCBTop, myCbr.Top
SaveSetting strAppName, strStartup, strCBLeft, myCbr.Left

' delete command bar

Call CBDeleteCommandBar(strMyCMB)

End Sub

'--------------------------------------------------------------------
Private Sub Workbook_Open()

' create command bar using position we had last time

Set myCbr = CreateCustomCommandBar(strMyCMB, _
GetSetting(strAppName, strStartup, strCBPosn, CBDefaultPosn), _
GetSetting(strAppName, strStartup, strCBTop, CBDefaultTop), _
GetSetting(strAppName, strStartup, strCBLeft, CBDefaultLeft))

' add button

Call AddButton(myCbr, strRun, strRunCode)

End Sub

'====================================================================
'
' Code Module
'
'====================================================================
Option Explicit

' Command Bar and Button Names

Public Const strMyCMB As String = "MyCommandBar"
Public Const strRun As String = "Run"
Public Const strRunCode As String = "RunCode"

' Registry Info

Public Const strAppName As String = "MyName"
Public Const strStartup As String = "Startup"
Public Const strCBPosn As String = "Position"
Public Const strCBTop As String = "Top"
Public Const strCBLeft As String = "Left"

' Default Command Bar Position

Public Const CBDefaultPosn As Long = msoBarTop
Public Const CBDefaultTop As Long = 0
Public Const CBDefaultLeft As Long = 0

'-------------------------------------------------------------------------
Function RunCode()
' Come here when the user click run button

MsgBox "Run Code"

End Function

'-------------------------------------------------------------------------
Function CreateCustomCommandBar(argCommandBar As String, _
argPosn As Long, argTop As Long, argLeft As Long) As CommandBar
' Create Custom CommandBar

Dim cbrNew As CommandBar
Dim ctlCBarControl As CommandBarControl
Dim myVar As String
Dim lngCnt As Long

If CBDoesCBExist(argCommandBar) Then
Call CBDeleteCommandBar(argCommandBar)
End If

Set cbrNew = Application.CommandBars.Add(Name:=argCommandBar)

With cbrNew
.Visible = True
.Position = argPosn
.Top = argTop
.Left = argLeft
End With

Set CreateCustomCommandBar = cbrNew

End Function

'-------------------------------------------------------------------------
Sub AddButton(argCbr As CommandBar, argCaption As String, argOnAction As String)
' add button to custom CommandBar

Dim ctlCBarControl As CommandBarControl

With argCbr
Set ctlCBarControl = .Controls.Add(msoControlButton)
With ctlCBarControl
.Caption = argCaption
.Style = msoButtonCaption
.BeginGroup = True
.Enabled = True
.OnAction = argOnAction
.Parameter = argCaption
End With
End With

End Sub

'-------------------------------------------------------------------------
Function CBDoesCBExist(strCBarName As String) As Boolean
' Check for existence of command bar with name strCBarName

Dim cbrBar As CommandBar

On Error Resume Next
Set cbrBar = CommandBars(strCBarName)
If Err = 0 Then
CBDoesCBExist = True
Else
CBDoesCBExist = False
End If

End Function

'-------------------------------------------------------------------------
Sub CBDeleteCommandBar(strCBarName As String)
' Delete the command bar specified by strCBarName. If the
' command bar does not exist, an error will occur and that
' error is ignored here.

On Error Resume Next
Application.CommandBars(strCBarName).Delete

End Sub

Peter Mo.
This is great...thank you
polska2180
reply 3

You can use google to search for other answers

 

More Articles

• VBA books
• RunTime Error 3704 Loading Recordset from Stored Procedure
• WIA: problem shooting picture
• Running a macro at a certain time
• I am searching a VBS scripting example for a free form path item ...
• VBA WORKSHEET
• Word: Normal.dot template menu bar
• excel vba
• Progress Bar
• Novice VBA User needs help with using data from input box for Cel...
Welcome to Bokebb   New Update   Joins the collection  
 

New Articles

• Filter string in GetOpenFilename method
• Reg: Need an API which reads GDI Object
• Hybrid between Cells.Select and ActiveCe
• MS Word Option Button Collection
• create a chart based on QueryTable
• Export excel sheet as text file with mac
• mysterious error: RUNTIME ERROR '451'
• API Used in VBA
• Left and Right Margins in special cases
• Does the VBA save word as XML???
• PPT VBA - Searching a file that is not o
• Excel macro to send email not running
• Excel: Copying cells under a specific ce
• Outlook new Mail item
• Referencing unregistered DLL

Hot Articles

• row height
• Own Right-Mouse-Click Menu item in Outl
• Run Time Error 1004 On Long Running Stor
• Function which get range arg.
• Accessing Win32 API in VB script(.VBS fi
• Distributing a Macro across an organizat
• Date Problem
• Code to call MS SQL Server 2000 SP
• Increase Speed of a Word Macro
• Passing new parameters to Report(.rdlc)
• reportbuilder
• Parsing text with tags in Excel using VBA
• Search all text objects - PowerPoint VBA
• Hyperlink to a subroutine
• Installing VBA6.0 on XP

Recommend Articles

• MS Access VBA: How to solve the problem,
• Needing extract elements XML to a listco
• Wanting generate a HTML output right to
• last Modified date in Excel spreadsheet
• VBA Syntax
• Task Scheduler
• disabling buttons in a list form
• Printing help
• Can a field result in a Query be formatt
• 'Call was rejected by callee' using VBA
• Visio VBA Macro to detect connector endp
• Vb migration to Access 2003
• Display Metadata of files
• VB Control Air card sent file to GPRS
• Can any1 arrange the ADO connection logi