Results 1 to 2 of 2
  1. #1
    3 Star Lounger
    Join Date
    Apr 2001
    Posts
    304
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Removing a toolbar IF it's there (VBA)

    I am trying to create a macro (for an Excel add-in) that will create and display a specialized toolbar upon opening a workbook if the Add-in is set to load with Excel. So far I have a Workbook _Open macro that calls a CreateBar macro that looks like this:

    Public Sub CreateBar()
    Dim oBar As CommandBar
    Dim oControl As CommandBarControl
    Set oBar = Application.CommandBars.Add
    oBar.Name = "Sales Journal Prep 2"
    oBar.Visible = True
    Set oControl = oBar.Controls.Add(ID:=1, Before:=1)
    oControl.OnAction = "InsertandCopy"
    oControl.Style = msoButtonCaption
    oControl.Caption = "InsertandCopy"
    oControl.TooltipText = "Inserts New Columns/Rows and copies info"
    Set oControl = Nothing
    Set oBar = Nothing
    Set oControl = oBar.Controls.Add(ID:=1, Before:=2)
    oControl.OnAction = "InsertIfStatementA"
    oControl.Style = msoButtonCaption
    oControl.Caption = "IF Statement A"
    oControl.TooltipText = "Inserts formula into Column A"
    Set oControl = Nothing
    Set oBar = Nothing
    Set oControl = oBar.Controls.Add(ID:=1, Before:=3)
    oControl.OnAction = "InsertIfStatementB"
    oControl.Style = msoButtonCaption
    oControl.Caption = "IF Statement B"
    oControl.TooltipText = "Inserts formula into column B"
    Set oControl = Nothing
    Set oBar = Nothing
    Set oControl = oBar.Controls.Add(ID:=1, Before:=4)
    oControl.OnAction = "PasteFormulasLoop"
    oControl.Style = msoButtonCaption
    oControl.Caption = "Paste Formulas"
    oControl.TooltipText = "Pastes formulas to non-empty rows"
    Set oControl = Nothing
    Set oBar = Nothing
    End Sub

    My first problem is, I'm getting an error at the part where I'm trying to add a 2nd button - I'm sure my code is wrong. I tried to just copy and past the first set of code and obviously that doesn't work.

    Second problem - I would like this toolbar to be deleted and recreated every time that a workbook is opened for 2 reasons. 1.) cause if you don't remove it, you'll get an error each time it attempts to create another upon opening and 2.) cause i may at some point make changes to the add-in and would like to make sure that they are getting the most up-to-date toolbar instead of Excel pulling the stored version of the toolbar from their local machine. I attempted to add a RemoveBar macro to get rid of it, then added a Workbook_BeforeClose macro to call the RemoveBar macro, BUT, when you open a workbook for the first time, if there is no toolbar there to remove, you get errors. So, I obviously need some sort of IF statement. IF toolbar is there, then remove. Otherwise, go on with CreateBar macro. But I'm not that talented. Any suggestions?

    Thanks - Satiria

  2. Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 16 Times in 16 Posts

    Re: Removing a toolbar IF it's there (VBA)

    1) You shouldn't set oBar to nothing after creating the first button, because you can't use it any more after that. In fact, you only need to set oBar and oControl to Nothing at the end of the code.

    2) Use code like this:

    ' Suppress error message if toolbar doesn't exist
    On Error Resume Next
    Application.CommandBars("Sales Journal Prep 2").Delete
    ' Restore normal error handling
    On Error GoTo 0

    If you add your own error handling, use

    On Error GoTo MyErrHandler

    (with the appropriate label name) instead of On Error GoTo 0.

Posting Permissions

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