【问题标题】:How to add Outlook toolbar under the subject line?如何在主题行下添加 Outlook 工具栏?
【发布时间】:2014-06-08 23:54:51
【问题描述】:

我需要使用 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
    }

}

【问题讨论】:

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


    【解决方案1】:

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

    很遗憾,无法在邮件正文和地址标头之间注入自定义 UI。您可以使用任务窗格和表单区域,但它们必须位于标题上方或邮件正文的左侧、右侧和底部。

    【讨论】:

    • 好的。至于第二个问题——不幸的是,这是真的。谢谢你的帮助。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      相关资源
      最近更新 更多