Search code examples
excelvbaexcel-addins

Delete Command Bars > 1 occurence


I have a third party add-in. My issue is, it counts the last command bar loaded and if not there, it re-loads it.

I do not want to edit their code as with updates etc., it would need changing every time.

Is there a way I can delete all command bars but one or change the load order to avoid this issue?

Public Sub ABCInitializeAddin()

Dim menuEntries As Integer
Dim lastIndex As Integer

' Get number of menu entries
menuEntries = Application.CommandBars(WorksheetMenuBar).Controls.Count

' Get index of last entry
lastIndex = Application.CommandBars(WorksheetMenuBar).Controls(menuEntries).Index

If Not Application.CommandBars(WorksheetMenuBar).Controls(lastIndex - 1).Caption = ABCMenuEntry Then

    ' Add main menu entry
    Dim ABCMainMenu As CommandBarControl
    Set ABCMainMenu = CommandBars(WorksheetMenuBar).Controls.Add(Type:=msoControlPopup, Before:=lastIndex)
    ABCMainMenu.Caption = ABCMenuEntry

End Sub

Solution

  • 
    Sub STDeleteXavex()
    
        Dim menuEntries As Integer
        Dim lastIndex As Integer
        Dim i As Long
        Dim p As Long
        Dim count As Long
    
        count = -1
    
        ' Get number of menu entries
        menuEntries = Application.CommandBars("Worksheet Menu Bar").Controls.count
    
        For i = 1 To menuEntries:
            If Application.CommandBars("Worksheet Menu Bar").Controls(i).Caption = "&ABC Online" Then count = count + 1
        Next i
    
        For p = 1 To count:
            Application.CommandBars("Worksheet Menu Bar").Controls("ABC Online").Delete
        Next p
    
    End Sub