locked
FAQs: 怎样在Outlook 2003 主窗口中添加一个命令栏(CommandBar)和按钮(buttons)? RRS feed

  • 问题

  • 为了帮助大家更好地学习 VSTO 技术,微软论坛技术支持团队编辑了一些列的 "VSTO 常见问题及解答" 精华帖。

    本帖的主题是:怎样在Outlook 2003 主窗口中添加一个命令栏(CommandBar)和按钮(buttons)?

    如果您觉得这个帖子对您的学习、工作有所帮助,请再把这个帖子分享给你的同学、同事和朋友。

    如果您想阅读更多的 "VSTO 常见问题及解答",请打开索引页面:
    http://social.msdn.microsoft.com/Forums/zh-CN/vstudiozhchs/thread/ed10f3de-40bb-4f16-81f9-25ace1f152ba

     


    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    2011年2月28日 9:30

答案

  • Outlook应用程序有两种类型的用户界面窗口:Explorer窗口和Inspector窗口。主要的用户界面是一个explorer,邮件/联系人/任务/会议窗口是Inspector类型的。我们可以通过调用this.Application.ActiveExplorer().CommandBars.Add(…)方法向Explorer窗口添加命令栏。

    但是为了向inspector窗口添加任务栏和按钮,我们需要监听Application.Inspectors.NewInspector事件。无论一个新的inspector是被创建或者显示,都会激发这个事件。在处理函数中,我们从参数中接收到一个inspector对象的实例。然后我们调用Inspector.CommandBars.Add(…)来创建任务栏。

    代码是这样的:

            Outlook.Inspectors inspectors = null;

            Office.CommandBar cb;

            Office.CommandBarComboBox edt;

            Office.CommandBarButton btn;

     

            private void ThisAddIn_Startup(object sender, System.EventArgs e)

            {

                inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(inspectors_NewInspector);

            }

     

            void inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)

            {

                cb = Inspector.CommandBars.Add("test", Office.MsoBarPosition.msoBarTop,

                    false, true) as Office.CommandBar;

                cb.Visible = true;

                cb.Protection = Microsoft.Office.Core.MsoBarProtection.msoBarNoCustomize;

                edt = cb.Controls.Add(Office.MsoControlType.msoControlEdit, missing, missing, missing, true) as Office.CommandBarComboBox;

                edt.Caption = "My Editor";

                edt.Tag = "My Editor";

                btn = cb.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true) as Office.CommandBarButton;

                btn.Caption = "My Button";

                }

    相关的论坛帖子:http://social.msdn.microsoft.com/forums/en-US/vsto/thread/4ca19753-9883-4a86-a825-36de4e3ba941/

    更多的有关VSTOFAQ,请看:

    http://social.msdn.microsoft.com/Forums/zh-CN/vstudiozhchs/thread/ed10f3de-40bb-4f16-81f9-25ace1f152ba


    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    2011年2月28日 9:31
  • 添加VB版本的代码,

    public class ThisAddIn

        Dim inspectors As Outlook.Inspectors

        Dim cb As Office.CommandBar

        Dim edt As Office.CommandBarComboBox

        Dim btn As Office.CommandBarButton

       

        Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

            inspectors = Application.Inspectors

            AddHandler inspectors.NewInspector, AddressOf inspectors_NewInspector

        End Sub

     

        Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector)

            cb = Inspector.CommandBars.Add("test", Office.MsoBarPosition.msoBarTop, False, True)

            cb.Visible = True

            cb.Protection = Microsoft.Office.Core.MsoBarProtection.msoBarNoCustomize

            edt = cb.Controls.Add(Office.MsoControlType.msoControlEdit, , , , True)

            edt.Caption = "My Editor"

            edt.Tag = "My Editor"

            btn = cb.Controls.Add(Office.MsoControlType.msoControlButton, , , , True)

            btn.Caption = "My Button"

            btn.Tag = "My Button"

        End Sub

    End Class


    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    2011年2月28日 9:31

全部回复

  • Outlook应用程序有两种类型的用户界面窗口:Explorer窗口和Inspector窗口。主要的用户界面是一个explorer,邮件/联系人/任务/会议窗口是Inspector类型的。我们可以通过调用this.Application.ActiveExplorer().CommandBars.Add(…)方法向Explorer窗口添加命令栏。

    但是为了向inspector窗口添加任务栏和按钮,我们需要监听Application.Inspectors.NewInspector事件。无论一个新的inspector是被创建或者显示,都会激发这个事件。在处理函数中,我们从参数中接收到一个inspector对象的实例。然后我们调用Inspector.CommandBars.Add(…)来创建任务栏。

    代码是这样的:

            Outlook.Inspectors inspectors = null;

            Office.CommandBar cb;

            Office.CommandBarComboBox edt;

            Office.CommandBarButton btn;

     

            private void ThisAddIn_Startup(object sender, System.EventArgs e)

            {

                inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(inspectors_NewInspector);

            }

     

            void inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)

            {

                cb = Inspector.CommandBars.Add("test", Office.MsoBarPosition.msoBarTop,

                    false, true) as Office.CommandBar;

                cb.Visible = true;

                cb.Protection = Microsoft.Office.Core.MsoBarProtection.msoBarNoCustomize;

                edt = cb.Controls.Add(Office.MsoControlType.msoControlEdit, missing, missing, missing, true) as Office.CommandBarComboBox;

                edt.Caption = "My Editor";

                edt.Tag = "My Editor";

                btn = cb.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true) as Office.CommandBarButton;

                btn.Caption = "My Button";

                }

    相关的论坛帖子:http://social.msdn.microsoft.com/forums/en-US/vsto/thread/4ca19753-9883-4a86-a825-36de4e3ba941/

    更多的有关VSTOFAQ,请看:

    http://social.msdn.microsoft.com/Forums/zh-CN/vstudiozhchs/thread/ed10f3de-40bb-4f16-81f9-25ace1f152ba


    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    2011年2月28日 9:31
  • 添加VB版本的代码,

    public class ThisAddIn

        Dim inspectors As Outlook.Inspectors

        Dim cb As Office.CommandBar

        Dim edt As Office.CommandBarComboBox

        Dim btn As Office.CommandBarButton

       

        Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

            inspectors = Application.Inspectors

            AddHandler inspectors.NewInspector, AddressOf inspectors_NewInspector

        End Sub

     

        Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector)

            cb = Inspector.CommandBars.Add("test", Office.MsoBarPosition.msoBarTop, False, True)

            cb.Visible = True

            cb.Protection = Microsoft.Office.Core.MsoBarProtection.msoBarNoCustomize

            edt = cb.Controls.Add(Office.MsoControlType.msoControlEdit, , , , True)

            edt.Caption = "My Editor"

            edt.Tag = "My Editor"

            btn = cb.Controls.Add(Office.MsoControlType.msoControlButton, , , , True)

            btn.Caption = "My Button"

            btn.Tag = "My Button"

        End Sub

    End Class


    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    2011年2月28日 9:31