Macro keeps creating additional toolbars

projectile

Board Regular
Joined
Dec 14, 2007
Messages
193
Hi

I have this code that creates a toolbar each time excel opens, however it is creating multiple instances of the macro toolbar in excel , see screen shot.

The items in yellow are duplicates of the ones highlighted in red. Each time I start up Excel, I think it tries to create another instance of the toolbars.

Is there anyway to initially add the toolbars when the addin is installed, and for the code not to keep adding toolbars if they exist.

Hope this makes sense.


mrexceltoolbar.png



Code:
Sub SaveToolbarPos()
With CommandBars("ABC")
SaveSetting "MyUtils", "MyToolbar", "Pos", .Position
SaveSetting "MyUtils", "MyToolbar", "Top", .Top
SaveSetting "MyUtils", "MyToolbar", "Left", .Left
SaveSetting "MyUtils", "MyToolbar", "RowIndex", .RowIndex
End With
'DeleteSetting "MyUtils", "MyToolbar"
End Sub

Sub SetToolbarPos()
With CommandBars("IDModeling")
.Position = GetSetting("MyUtils", "MyToolbar", "Pos", msoBarFloating)
.Top = GetSetting("MyUtils", "MyToolbar", "Top", 1)
.Left = GetSetting("MyUtils", "MyToolbar", "Left", 1)
.RowIndex = GetSetting("MyUtils", "MyToolbar", "RowIndex", 1)
End With
End Sub

Option Explicit

Public Const ToolBarName As String = "House Bill"

'===========================================
Sub Auto_Open()
    Call CreateMenubar
End Sub

'===========================================
Sub Auto_Close()
    Call RemoveMenubar
End Sub

'===========================================
Sub RemoveMenubar()
    On Error Resume Next
    Application.CommandBars(ToolBarName).Delete
    On Error GoTo 0
End Sub

'===========================================
Sub CreateMenubar()

    Dim iCtr As Long

    Dim MacNames As Variant
    Dim CapNamess As Variant
    Dim TipText As Variant

    Call RemoveMenubar

    MacNames = Array("HouseBill")

    CapNamess = Array("Create EDI House Bill File")

    TipText = Array("HouseBill tip")

    With Application.CommandBars.Add
     '   .Name = ToolBarName
      '  .Left = 200
       ' .Top = 50
      '  .Protection = msoBarNoProtection
      '  .Visible = True
      '  .Position = msoBarFloating

        For iCtr = LBound(MacNames) To UBound(MacNames)
            With .Controls.Add(Type:=msoControlButton)
                .OnAction = "'" & ThisWorkbook.Name & "'!" & MacNames(iCtr)
                .Caption = CapNamess(iCtr)
                .Style = msoButtonIconAndCaption
                .FaceId = 71 + iCtr
                .TooltipText = TipText(iCtr)
            End With
        Next iCtr
    End With
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
A couple of thoughts:

1) Depending on the version of Excel, I'd suggest you use Workbook events rather than Auto_Open and Auto_Close. E.g. Workbook_Open or Workbook_AddInInstall instead of Auto_Open.

2) Amend the delete macro to cater for multiple instances, e.g.
Code:
Sub RemoveMenubar()
    Dim bar As CommandBar
    
    For Each bar In Application.CommandBars
        If bar.Name = ToolBarName Then bar.delete
    Next bar
End Sub

3) With (2) in situ, insert a line at the top of CreateMenubar thats calls RemoveMenubar first. Then multiple instances will be detected (and removed) whenever you attempt to create the bar too.
 
Last edited:
Upvote 0
Hi

I think I done as you suggested, however it is still creating multiple instances;

Any ideas where I have gone wrong?
33540378.gif
 
Upvote 0
I'm surprised you're still getting multiple instances.

Another idea - and a bit of a stab in the dark if I'm honest! Toolbar problems suggests there may be problems with a file called excel11.xlb (the numbers change with the version of Excel). If you don't mind resetting all the built-in toolbars to default settings then you can simply delete this file, but you must quit Excel before doing so. On my computer it's saved in C:\Documents and Settings\(Username)\Application Data\Microsoft\Excel but that could be different on yours.

NB: If Excel is open when you delete this file then it will come back automatically!
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,543
Members
449,316
Latest member
sravya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top