views:

678

answers:

3

Hello,

We have been developing an Outlook Add-in using Visual Studio 2008. However I am facing a strange behavior while adding a command button to a custom command bar. This behavior is reflected when we add the button in the reply, reply all and forward windows. The issue is that the caption of the command button is not visible though when we debug using VS it shows the caption correctly. But the button is captionless when viewed in Outlook(2003).

I have the code snippet as below. Any help would be appreciated.

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");
            }
        }
+1  A: 

I don't know the answer to your question, but I would highly recommend Add-In Express for doing the addin. See http://www.add-in-express.com/add-in-net/. I've used this in many projects, including some commercial software and it is completely awesome.

It does all the Outlook (and office) integration for you so you just work with it like any toolbar and just focus on the specifics of what you need it to do. You won't ever have to worry about the Outlook extensibility at all. Highly recommended.

Anyway, just wanted to mention it as something to look in to. It will definitely save some headaches if you're comfortable with using a 3rd party component in the project.

Ryan Farley
A: 

I don't know, but your code raises two questions:

  1. Why are you declaring "CommandBarControl myCommandbarButton" instead of "CommandBarButton myCommandbarButton"?

  2. Why are you setting the width to 900 pixels? That's huge. I never bother with this setting in Excel since it autosizes, and I'm guessing the Outlook would behave the same.

Nick Hebb
A: 

You aren't setting the command bar button's style property (from what I can tell).

This results in the button having an MsoButtonStyle of msoButtonAutomation. I have seen the caption fail to appear if the style is left at this.

Try setting the Style property to msoButtonCaption.