Custom Command bar options not working


New Member
Mar 19, 2009

please see the following code.
I have create two custom CommandBarPopup on the context menu each with 2 CommandBarButton (with click events on them).

After run my solution, I am able to see my custom options in the Context Menu.

I am facing 2 problems:
1. The major problem is, when I run the solution, I click on one of the 4 CommandBarButtons and the click event is called, doing the assigned process. But, if click on any of the CommandBarButtons after that, my click events dont get called at all, unless restart executing my project.

But, even after restarting the project, only the CommandBarButton click event works only for the first time, no matter how much I try.

Consider I have 2 CommandBarPopups - A and B.
A has A1 and A2 as CommandBarButtons
B has B1 and B2 as CommandBarButtons

When I start the project and I click on any button A1, A2, B1 or B2, the respective button's click event will work. But after that, if I click on any of the buttons A1, A2, B1 or B2, nothing works.

Please tell me, what am I doing wrong here?

2. After I close my project, and open excel directly, still I can see the custom options in the context menu, even though I do the RESET() method on all the command bars. Can you tell whats wrong here?

private void ThisAddIn_Startup(object sender, EventArgs e)


private void ThisAddIn_Shutdown(object sender, EventArgs e)

/// <summary>
/// Initializes and insert the custom context menu items
/// </summary>
void InsertContextMenu()
            var commandBarTools = Application.CommandBars["Cell"];
            //add Source Menu to the Context Menu
            var cmdBarTools_Src = commandBarTools.Controls.Add(MsoControlType.msoControlPopup, 1, "", 1, true);
            cmdBarTools_Src.Visible = true;
            cmdBarTools_Src.Caption = "Source";

            //Select Source1 sub-option
            var popup_src1 = (CommandBarPopup)cmdBarTools_Src;
            var bar_src1 = popup_src1.CommandBar;
            var cmdBarControl_src1 = bar_src1.Controls.Add(MsoControlType.msoControlButton, 1, "", 1, true);
            cmdBarControl_src1.Caption = "Select Source 1";
            var button_src1 = (CommandBarButton)cmdBarControl_src1;
            button_src1.Click += button_src1_Click;

            //Select Source2 sub-option
            var popup_src2 = (CommandBarPopup)cmdBarTools_Src;
            var bar_src2 = popup_src2.CommandBar;
            var cmdBarControl_src2 = bar_src2.Controls.Add(MsoControlType.msoControlButton, 1, "", 2, true);
            cmdBarControl_src2.Caption = "Select Source 2";
            var button_src2 = (CommandBarButton)cmdBarControl_src2;
            button_src2.Click += button_src2_Click;

            //Select Mapping sub-option
            var popup_map = (CommandBarPopup)cmdBarTools_Src;
            var bar_map = popup_map.CommandBar;
            var cmdBarControl_map = bar_map.Controls.Add(MsoControlType.msoControlButton, 1, "", 3, true);
            cmdBarControl_map.Caption = "Select Mapping";
            var button_map = (CommandBarButton)cmdBarControl_map;
            button_map.Click += button_map_Click;

            //add Constraints Menu to the Context Menu
            var cmdBarTools_Cnst = commandBarTools.Controls.Add(MsoControlType.msoControlPopup, 1, "", 2, true);
            cmdBarTools_Cnst.Visible = true;
            cmdBarTools_Cnst.Caption = "Constraints";

            //Select Add\Edit Constraint sub-option
            var popup_addCstr = (CommandBarPopup)cmdBarTools_Cnst;
            var bar_addCstr = popup_addCstr.CommandBar;
            var cmdBarControl_addCstr = bar_addCstr.Controls.Add(MsoControlType.msoControlButton, 1, "", 1, true);
            cmdBarControl_addCstr.Caption = "Add\\Edit Constraint";
            var button_addCstr = (CommandBarButton)cmdBarControl_addCstr;
            button_addCstr.Click += button_addCstr_Click;

            //Select Delete Constraint
            var popup_delCstr = (CommandBarPopup)cmdBarTools_Cnst;
            var bar_delCstr = popup_delCstr.CommandBar;
            var cmdBarControl_delCstr = bar_delCstr.Controls.Add(MsoControlType.msoControlButton, 1, "", 2, true);
            cmdBarControl_delCstr.Caption = "Delete Constraint";
            var button_delCstr = (CommandBarButton)cmdBarControl_delCstr;
            button_delCstr.Click += button_delCstr_Click;

private void ResetCommandBars()
            for (var i = 1; i <= Application.CommandBars.Count; i++)


Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Latest member

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
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 "".
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