Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7729057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:52:25+00:00 2026-06-01T05:52:25+00:00

This has already been asked here , but I am just not satisfied with

  • 0

This has already been asked here, but I am just not satisfied with the answer given.

I am currently adding a custom context menu to Outlook. The code is as follows:

void Application_ItemContextMenuDisplay(Microsoft.Office.Core.CommandBar CommandBar, Microsoft.Office.Interop.Outlook.Selection Selection)
    {
        if (Online)
        {
            foreach (string category in FilingRuleManager.FilingRuleCategories)
            {
                Office.CommandBarPopup cb = CommandBar.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, missing, true) as Office.CommandBarPopup;
                cb.BeginGroup = true;
                cb.Visible = true;
                cb.Tag = MENUNAME;
                cb.Caption = category;
                //now add the filing rules as a sub menu
                foreach (FilingRuleDB rule in FilingRuleManager.FilingRules.Values)
                {
                    if (rule.RuleCategory == category)
                    {
                        Office.CommandBarButton cbSub = cb.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true) as Office.CommandBarButton;
                        _FilingRules.Add(cbSub);
                        cbSub.Visible = true;
                        cbSub.Caption = rule.RuleName;
                        cbSub.Tag = rule.FilingRuleID.ToString();
                        cbSub.Click += new Office._CommandBarButtonEvents_ClickEventHandler(FilingRules_Click);
                    }
                }
            }
        }
    }

When i run the application, each time i show the context menu in Outlook the Click handler (FilingRules_Click) is fired that many times. So if i right click 3 times, the handler is executed 3 times and so on.

There must be a better way to acheive this than the hack in the question linked above.

I have tried:

  1. Removing the CommandBarButtons just before adding them – however they do not exist!! as every time the Outlook Context menu is hidden, the custom items are automatically removed.
  2. Storing the controls in a List and then attempting to remove the handlers – this gives an AV as the buttons no longer exist after the menu is hidden.
  3. There is no ItemContextMenuHidden() event for me to hook into otherwise i would have tried that.
  4. Adding the items when the Add-in starts (i.e. only once with no ItemContextMenuDisplay() handler), however the items never appear due to the fact that they always get cleared when the menu is shown.

Anyone out there have another suggestion?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:52:27+00:00Added an answer on June 1, 2026 at 5:52 am

    Solved the problem.

    1. define the commandbutton as class level static variable
    2. attachment event ContextMenuClose for Outlook.Application.

      outlookInstance.ContextMenuClose += new ApplicationEvents_11_ContextMenuCloseEventHandler(outlookInstance_ContextMenuClose);
      
    3. implement the method as code

      void outlookInstance_ContextMenuClose(OlContextMenu ContextMenu)
      {
          if (ContextMenu == OlContextMenu.olItemContextMenu)
          {
              ContextIndexButton.Click -= new _CommandBarButtonEvents_ClickEventHandler(<your method>);
              ContextIndexButton = null;
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this has already been asked here but the answer (using a
I'm sure this has been asked here already but I can't locate the answer...
This has already been asked Here , but not by me and the OP
This has already been asked here , but I was looking for a solution
This has already been asked here but I was hoping there was a nicer
This question has been asked here in one form or another but not quite
I apologise if this question has already been asked on here before but I
Sorry if this has already been asked, but I just want to make sure
I know that this question has already been asked HERE but sadly none of
It's possible that this question has already been asked, but I can't seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.