C# CommandBarButton question

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 magnifying glasses in them. What am I doing wrong Is there a resource that explains the C# "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 question

  • EdW

    Thank you Peter,

    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.



  • Roni Schuetz

    Hi Blood,

    Are you using a VSTO Outlook Addin as below.
    http://www.outlookcode.com/vsto/toolbarpos.htm
    or an COM Addin for Outlook as below.
    http://support.microsoft.com/kb/302896/en-us/

    Can you show more code about how did you get the inspector instance

    Here is a link for your reference about the consideration when using Word as Editor.
    OL: How to Use CommandBars in Outlook Solutions
    http://support.microsoft.com/default.aspx scid=KB;en-us;Q201095

    CommandBarButtonClass is a wrapper for Office VBA help. You may consult the VBA Object Modal reference below.

    <Program Files>\Microsoft Office\OFFICE11\1033\VBAOL11.CHM

    <Program Files>\Microsoft Office\OFFICE11\1033\VBAOF11.CHM

    If you still have any concern, please feel free to post here.

    Best regards,
    Peter Huang



  • C# CommandBarButton question