How do I find the add-in controls on CommandBar?

markus_zhang

New Member
Joined
Jan 11, 2018
Messages
3
Hi Experts, I made an add-in and am trying to figure out how to remove the custom controls safely:

Code:
Private Sub Workbook_AddinInstall()
    Set cBackupRawControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add
    With cBackupRawControl
        .Caption = "Backup Raw Data"
        .Style = msoButtonCaption
        .OnAction = "BackupRaw"
    End With
    
    Set cBacktestControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add
    With cBacktestControl
        .Caption = "Back Test"
        .Style = msoButtonCaption
        .OnAction = "test"
    End With
End Sub

Code:
Private Sub Workbook_AddinUninstall()
    Dim cBRControl, cBTControl As CommandBarButton
    'Set cBRControl = CommandBars.FindControls(ID:="Backup Raw Data")
    'Set cBTControl = CommandBars.FindControls(ID:="Backup Test")
    
    'If cBRControl Then
        Application.CommandBars("Worksheet Menu Bar").Controls("Backup Raw Data").Delete
    'End If
    'If cBTControl Then
        Application.CommandBars("Worksheet Menu Bar").Controls("Back Test").Delete
    'End If
End Sub

You can see that in AddinUninstall I'd like to confirm the existence of both controls before removing them. The FindControls method doesn't work as I cannot search the controls by name. I could search for ID, but again my Add-in will be used on may other computers so they may have different ID. So my question is, is there a way to check the existence of both Controls before removing them? Thanks!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
The usual way is to just ignore errors:

Code:
Private Sub Workbook_AddinUninstall()
    On error resume next 
        Application.CommandBars("Worksheet Menu Bar").Controls("Backup Raw Data").Delete
        Application.CommandBars("Worksheet Menu Bar").Controls("Back Test").Delete
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,869
Messages
6,175,087
Members
452,611
Latest member
bls2024

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