使用.NET的Outlook加载项

时间:2020-03-05 18:42:23  来源:igfitidea点击:

我们已经在使用Visual Studio 2008开发Outlook加载项。但是,在向自定义命令栏添加命令按钮时,我遇到了奇怪的行为。当我们在回复中添加按钮,回复所有和转发窗口时,就会反映出这种行为。问题是命令按钮的标题不可见,尽管当我们使用VS进行调试时,它可以正确显示该标题。但是在Outlook(2003)中查看时,该按钮没有标题。

我有如下代码片段。任何帮助,将不胜感激。

private void AddButtonInNewInspector(Microsoft.Office.Interop.Outlook.Inspector inspector)
        {
            try
            {
                if (inspector.CurrentItem is Microsoft.Office.Interop.Outlook.MailItem)
                {

                    try
                    {                       
                        foreach (CommandBar c in inspector.CommandBars)
                        {
                            if (c.Name == "custom")
                            {
                                c.Delete();
                            }
                        }
                    }
                    catch
                    {
                    }
                    finally
                    {
                        //Add Custom Command bar and command button.
                        CommandBar myCommandBar = inspector.CommandBars.Add("custom", MsoBarPosition.msoBarTop, false, true);
                        myCommandBar.Visible = true;

                        CommandBarControl myCommandbarButton = myCommandBar.Controls.Add(MsoControlType.msoControlButton, 1, "Add", System.Reflection.Missing.Value, true);                        
                        myCommandbarButton.Caption = "Add Email";
                        myCommandbarButton.Width = 900;
                        myCommandbarButton.Visible = true;
                        myCommandbarButton.DescriptionText = "This is Add Email Button";

                        CommandBarButton btnclickhandler = (CommandBarButton)myCommandbarButton;
                        btnclickhandler.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.OnAddEmailButtonClick);
                    }

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "AddButtInNewInspector");
            }
        }

解决方案

回答

我不知道我们问题的答案,但我强烈建议我们使用Add-In Express进行加载。请参阅http://www.add-in-express.com/add-in-net/。我已经在许多项目中使用了此功能,包括一些商业软件,它非常棒。

它为我们完成了所有Outlook(和Office)集成,因此我们可以像使用任何工具栏一样使用它,并专注于我们需要执行的操作的细节。我们根本不必担心Outlook的可扩展性。强烈推荐。

无论如何,只是想提一下它是值得研究的东西。如果我们愿意在项目中使用第3方组件,则肯定会省去一些麻烦。

回答

我不知道,但是代码提出了两个问题:

  • 为什么要声明" CommandBarControl myCommandbarButton"而不是" CommandBarButton myCommandbarButton"?
  • 为什么将宽度设置为900像素?好大由于它会自动调整大小,因此我从不理会Excel中的此设置,并且我猜想Outlook的行为也一样。

回答

我们没有设置命令栏按钮的style属性(据我所知)。

这将导致按钮的MsoButtonStyle为msoButtonAutomation。我已经看到,如果样式保留在此处,标题将不会出现。

尝试将Style属性设置为msoButtonCaption。