Question

I need to write an Outlook 2003-2010 plugin using C# that adds two buttons to the Message Ribbon Bar (#1 on the picture) and a several buttons toolbar or a Form Region under the "Subject" line (#2 on the picture).

See image here

Currently I managed to add a button to the Ribbon toolbar, but it appears in the "Add-ins" Ribbon Bar. How do I add buttons to the "Message" Ribbon Bar?

And how do I add the Toolbar #2 ? I currently have no clue about how to add it.

Please help me!

I now have the follwing code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;

namespace SendLaterToolbar
{
    public partial class ThisAddIn
    {
        Office.CommandBar newToolBar;
        Office.CommandBarButton firstButton;
        Office.CommandBarButton secondButton;
        Outlook.Explorers selectExplorers;
        Outlook.Inspectors inspectors;
        Office.CommandBarButton _objEmailToolBarButton;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            selectExplorers = this.Application.Explorers;
            inspectors = this.Application.Inspectors;
            selectExplorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler(newExplorer_Event);
            inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(AddToEmail);
            AddToolbar();
        }

        private void newExplorer_Event(Outlook.Explorer new_Explorer)
        {
            ((Outlook._Explorer)new_Explorer).Activate();
            newToolBar = null;
            AddToolbar();
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {

        }

        private void AddToolbar()
        {

            if (newToolBar == null)
            {
                Office.CommandBars cmdBars =
                    this.Application.ActiveExplorer().CommandBars;
                newToolBar = cmdBars.Add("NewToolBar",
                    Office.MsoBarPosition.msoBarTop, false, true);
            }
            try
            {
                Office.CommandBarButton button_1 =
                    (Office.CommandBarButton)newToolBar.Controls
                    .Add(1, missing, missing, missing, missing);
                button_1.Style = Office
                    .MsoButtonStyle.msoButtonCaption;
                button_1.Caption = "Button 1";
                button_1.Tag = "Button1";
                if (this.firstButton == null)
                {
                    this.firstButton = button_1;
                    firstButton.Click += new Office.
                        _CommandBarButtonEvents_ClickEventHandler
                        (ButtonClick);
                }

                Office.CommandBarButton button_2 = (Office
                    .CommandBarButton)newToolBar.Controls.Add
                    (1, missing, missing, missing, missing);
                button_2.Style = Office
                    .MsoButtonStyle.msoButtonCaption;
                button_2.Caption = "Button 2";
                button_2.Tag = "Button2";
                newToolBar.Visible = true;
                if (this.secondButton == null)
                {
                    this.secondButton = button_2;
                    secondButton.Click += new Office.
                        _CommandBarButtonEvents_ClickEventHandler
                        (ButtonClick);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddToEmail(Microsoft.Office.Interop.Outlook.Inspector Inspector)
        {
            Outlook.MailItem _ObjMailItem = (Outlook.MailItem)Inspector.CurrentItem;

            if (Inspector.CurrentItem is Outlook.MailItem)
            {
                _ObjMailItem = (Outlook.MailItem)Inspector.CurrentItem;
                bool IsExists = false;

                foreach (Office.CommandBar _ObjCmd in Inspector.CommandBars)
                {
                    if (_ObjCmd.Name == "MyEmailToolBar")
                    {
                        IsExists = true;
                        _ObjCmd.Delete();
                    }
                }

                Office.CommandBar _ObjCommandBar = Inspector.CommandBars.Add("MyEmailToolBar", Office.MsoBarPosition.msoBarBottom, false, true);
                _objEmailToolBarButton = (Office.CommandBarButton)_ObjCommandBar.Controls.Add(Office.MsoControlType.msoControlButton, 1, missing, missing, true);

                if (!IsExists)
                {
                    _objEmailToolBarButton.Caption = "My Email ToolBar Button";
                    _objEmailToolBarButton.Style = Office.MsoButtonStyle.msoButtonIconAndCaptionBelow;
                    _objEmailToolBarButton.FaceId = 500;
                    _objEmailToolBarButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(_objEmailToolBarButton_Click);
                    _ObjCommandBar.Visible = true;
                }
            }
        }


        private void ButtonClick(Office.CommandBarButton ctrl, ref bool cancel)
        {
            MessageBox.Show("You clicked: " + ctrl.Caption);
        }

        private void _objEmailToolBarButton_Click(Office.CommandBarButton ctrl, ref bool cancel)
        {
            MessageBox.Show("My Email ToolBar ...");
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }

}

Was it helpful?

Solution

The custom buttons you are creating are meant to be hosted on a CommandBar control for Outlook 2000-2003. You can use the Ribbon designer in your VSTO project to add a custom Ribbon Group to the Message tab (the ID for which is TabNewMailMessage).

Unfortunately, there's no way to inject a custom UI between the message body and the address header. You can use Task Panes and Form Regions but they must go above the header or to the left, right and bottom of the message body.

OTHER TIPS

You might be able to display your own toolbar control under the Subject text box using Add-in Express. Have a look at Add-In Express Regions for Microsoft Outlook and VSTO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top