如何在主题行下添加Outlook工具栏?

时间:2014-04-23 22:24:13

标签: c# outlook ms-office outlook-addin outlook-vba

我需要使用C#编写一个Outlook 2003-2010插件,将两个按钮添加到消息功能区栏(图片上的#1)以及几个按钮工具栏或表单区域在“主题”行下(图片上的#2)

See image here

目前,我设法在功能区工具栏中添加了一个按钮,但它出现在“加载项”功能区栏中。 如何在“消息”功能区栏中添加按钮?

如何添加工具栏#2?我目前还不知道如何添加它。

请帮助我!

我现在有以下代码:

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
    }

}

2 个答案:

答案 0 :(得分:0)

您正在创建的自定义按钮旨在托管在Outlook 2000-2003的CommandBar控件上。您可以使用VSTO项目中的功能区设计器将自定义功能区组添加到“消息”选项卡(其标识为TabNewMailMessage)。

不幸的是,没有办法在邮件正文和地址标题之间注入自定义UI。您可以使用任务窗格和表单区域,但它们必须位于标题之上或邮件正文的左侧,右侧和底部。

答案 1 :(得分:0)

您可以使用Add-in Express在“主题”文本框下显示自己的工具栏控件。看看Add-In Express Regions for Microsoft Outlook and VSTO