Solved

C# Outlook 2007 Add-In - Custom Menu Item

Posted on 2008-10-16
4
3,354 Views
Last Modified: 2013-11-08
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
Comment
Question by:CyberUnDead
  • 2
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
Mihai Stancescu earned 300 total points
ID: 22738118
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
 

Author Comment

by:CyberUnDead
ID: 22740271
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
 
LVL 11

Assisted Solution

by:Mihai Stancescu
Mihai Stancescu earned 300 total points
ID: 22740351
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
 

Author Closing Comment

by:CyberUnDead
ID: 31506765
Awesome worked great the first time!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now