C# CommandBarButton problem

I'm using a VSTO Outlook 2003 Add-in. CommandBarButtons are displaying correctly in OutLook 2003 except when Word is used as the mail editor, (default installation).

When Word is the editor the buttons display as small boxes with "x"s in them. What am I doing wrong Is there a resource explains in C# the "Use" of CommandBarButtonClass Members Below is a snippet of the C# code which I'm having the problem with.

// Begin snippet

// Add the CommandBar

newBar = inspector.CommandBars.Add("ExportControlBar", Office.MsoBarPosition.msoBarTop, false, true);

// Add a button

newButton = (Office.CommandBarButton)newBar.Controls.Add(

Office.MsoControlType.msoControlButton, 1, missing, missing, true);

newButton.Caption = "Export Data.";

newButton.FaceId = 172;

newButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(newButton_Click);

newBar.Visible = true;

// End snippet

Thanks for any help,

Ron Blood |<:^)




Answer this question

C# CommandBarButton problem

  • Fantasio

    I have solved my problem. It was very important to specify the correct "newButton.Style". See below. The ComandBarButton contains 49 private, 20 public and reserved, a total of 69 members! It was difficult to figure out "what" to pass to some of these members as they are sparsely documented. I literally stumbled into the solution by looking at many different C# code examples :)

    // Begin Sample C#, Add a CommandBarButton to all instances of Inspectors.

    using System;

    using System.Windows.Forms;

    using Microsoft.VisualStudio.Tools.Applications.Runtime;

    using Outlook = Microsoft.Office.Interop.Outlook;

    using Office = Microsoft.Office.Core;

    namespace OutlookAddin

    {

    public partial class ThisApplication

    {

    private Office.CommandBar newBar;

    private Office.CommandBarButton newButton;

    private Outlook.Inspectors inspectors;

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

    {

    inspectors = this.Inspectors;

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

    }

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

    {

    }

    private void inspectors_NewInspector(Outlook.Inspector inspector)

    {

    // Check for previous instance of CommandBar, don't add it if it exists

    for (int i = 1; i <= inspector.CommandBars.Count; i++)

    if (inspector.CommandBarsIdea.Name == "ExportControlBar") return;

    // Add the CommandBar

    newBar = inspector.CommandBars.Add("ExportControlBar", Office.MsoBarPosition.msoBarTop, false, true);

    // Add a button

    newButton = (Office.CommandBarButton)newBar.Controls.Add(

    Office.MsoControlType.msoControlButton, 1, missing, missing, true);

    newButton.Caption = "Export data";

    newButton.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;

    newButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(newButton_Click);

    newBar.Visible = true;

    }

    // Display a Message Box in response to button click.

    private void newButton_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)

    {

    MessageBox.Show("Exporting Data");

    }

    #region VSTO generated code

    #endregion

    #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(ThisApplication_Startup);

    this.Shutdown += new System.EventHandler(ThisApplication_Shutdown);

    }

    #endregion

    }

    }

    // End Sample C#, Add a CommandBarButton to all instances of Inspectors.



  • C# CommandBarButton problem