Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3371
  • Last Modified:

C# Outlook 2007 Add-In - Custom Menu Item

I am having difficulty attaching my custom menu to the appropriate window.  Using a host of samples and explanations from Google results I managed to put together what is below.   When an inspector (MailItem) event occurs my menu is added but it is added to the main menu instead of the inspector window.  The excerpt below contains my NewInspector event which triggers my AddMenu routine.  My goal is to attach the custom menu to the inspector (mailitem) window.
        private Office.CommandBar menuCommandBar;
        private Office.CommandBarButton menuCommandBarButton;
        private Outlook.Inspectors Inspector;        
        
        private void Inspectors_NewInspector(Outlook.Inspector Inspector)
        {
            if (Inspector.CurrentItem is Outlook.MailItem)
            {
                    AddMenu();
            }
        }
        
        private void AddMenu()
        {
            try
            {
                menuCommandBar = Application.ActiveExplorer().CommandBars.Add(menuTag, Office.MsoBarPosition.msoBarTop, false, true);
              
                if (menuCommandBar != null)
                {
                    menuCommandBar.Visible = true;
                }
 
                menuCommandBarButton = ((Office.CommandBarButton)(menuCommandBar.Controls.Add(Office.MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true)));
                menuCommandBarButton.Caption = menuTitle;
                menuCommandBarButton.Style = Office.MsoButtonStyle.msoButtonCaption;
                menuCommandBarButton.Visible = true;
                menuCommandBarButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(buttonCustomAddIn_Click);
 
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

Open in new window

0
CyberUnDead
Asked:
CyberUnDead
  • 2
  • 2
2 Solutions
 
Mihai StancescuSoftware Engineer Commented:
Try this:

Hope this helps!

Regards,
Mishu
private void Inspectors_NewInspector(Outlook.Inspector Inspector)
        {
            if (Inspector.CurrentItem is Outlook.MailItem)
            {
                    AddMenu(Inspector);
            }
        }
 
private void AddMenu(Outlook.Inspector CurrentInspector)
        {
            try
            {
                menuCommandBar = CurrentInspector.CommandBars.Add(menuTag, Office.MsoBarPosition.msoBarTop, false, true);
              
                if (menuCommandBar != null)
                {
                    menuCommandBar.Visible = true;
                }
 
                menuCommandBarButton = ((Office.CommandBarButton)(menuCommandBar.Controls.Add(Office.MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true)));
                menuCommandBarButton.Caption = menuTitle;
                menuCommandBarButton.Style = Office.MsoButtonStyle.msoButtonCaption;
                menuCommandBarButton.Visible = true;
                menuCommandBarButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(buttonCustomAddIn_Click);
 
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

Open in new window

0
 
CyberUnDeadAuthor Commented:
Mishu007:

The above code works great.  I was wondering if I could ask a related follow-up question.  How do I ensure that I appropriately remove the command bar?  I am getting multiples of my custom button.
private void RemoveMenu(Outlook.Inspector CurrentInspector)
        {
            try
            {
                //Office.CommandBarButton menuComandBarOld = (Office.CommandBarButton)CurrentInspector.CommandBars.FindControl(Office.MsoControlType.msoControlButton, Type.Missing, menuTag, true);
                Office.CommandBar menuComandBarOld = (Office.CommandBar)CurrentInspector.CommandBars.FindControl(Type.Missing, Type.Missing, menuTag, true);
 
                if (menuComandBarOld != null)
                {
                    menuCommandBar.Delete();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message.ToString(), " Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

Open in new window

0
 
Mihai StancescuSoftware Engineer Commented:
Regards,
Mishu
private void RemoveMenu(Outlook.Inspector CurrentInspector)
        {
            try
            {
                Office.CommandBar menuComandBarOld = (Office.CommandBar)CurrentInspector.CommandBars["YoumenuName"];
 
                if (menuComandBarOld != null)
                {
                    menuCommandBar.Delete();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message.ToString(), " Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

Open in new window

0
 
CyberUnDeadAuthor Commented:
Awesome worked great the first time!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now