Consulting

Results 1 to 6 of 6

Thread: Solved: Adding a New Toolbar

  1. #1
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location

    Solved: Adding a New Toolbar

    Is there anyway to create a new toolbar and have it associated and linked to a specific worksheet so that when a worksheet is sent via email to someone adn that person opens the worksheet, the toolbar is available? I don't want/need it to be available for other worksheets as all of the macros linked to the buttons apply only to one specific worksheet.

    I tried adding buttons to the actual worksheet but it has become rather cluttered with buttons. Looking for a better solution.

    Opv

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Here is an example

    [vba]

    Option Explicit

    Const TOLBAR_NAME As String = "MyToolbar"

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call DeleteToolbar
    End Sub

    Private Sub Workbook_Open()
    Call DeleteToolbar

    Call CreateToolbar
    End Sub

    Private Sub CreateToolbar()

    With Application.CommandBars.Add(Name:=ToggleFormsDesign, temporary:=True)

    With .Controls.Add(Type:=msoControlButton)

    .Caption = "Button1"
    .FaceId = 29
    .OnAction = "procButton1"
    End With

    With .Controls.Add(Type:=msoControlButton)

    .Caption = "Button2"
    .FaceId = 30
    .OnAction = "procButton2"
    End With

    With .Controls.Add(Type:=msoControlButton)

    .Caption = "Button3"
    .FaceId = 31
    .OnAction = "procButton3"
    End With

    .Position = msoBarTop
    .Visible = True
    End With
    End Sub

    Private Sub DeleteToolbar()
    On Error Resume Next
    Application.CommandBars(ToggleFormsDesign).Delete
    End Sub
    [/vba]

    This is workbook event code.
    To input this code, right click on the Excel icon on the worksheet
    (or next to the File menu if you maximise your workbooks),
    select View Code from the menu, and paste the code
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Thanks. I'm receiving an error in the DeleteToolBar subroutine that "ToggleFormsDesign" is undefined.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Picked the wrong thing from intellisense

    [vba]

    Option Explicit

    Const TOLBAR_NAME As String = "MyToolbar"

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call DeleteToolbar
    End Sub

    Private Sub Workbook_Open()
    Call DeleteToolbar

    Call CreateToolbar
    End Sub

    Private Sub CreateToolbar()

    With Application.CommandBars.Add(Name:=TOLBAR_NAME, temporary:=True)

    With .Controls.Add(Type:=msoControlButton)

    .Caption = "Button1"
    .FaceId = 29
    .OnAction = "procButton1"
    End With

    With .Controls.Add(Type:=msoControlButton)

    .Caption = "Button2"
    .FaceId = 30
    .OnAction = "procButton2"
    End With

    With .Controls.Add(Type:=msoControlButton)

    .Caption = "Button3"
    .FaceId = 31
    .OnAction = "procButton3"
    End With

    .Position = msoBarTop
    .Visible = True
    End With
    End Sub

    Private Sub DeleteToolbar()
    On Error Resume Next
    Application.CommandBars(TOLBAR_NAME).Delete
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Works like a charm. Thanks for the help!

    Opv

  6. #6
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    I will repost my followup question as a new thread since this topic has been marked "Solved."

    Opv
    Last edited by Opv; 06-14-2010 at 04:37 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •