Microsoft KB Archive/303018

= HOWTO: Use Automation to Create Office Command Bars and Controls with Visual C# .NET =

Article ID: 303018

Article Last Modified on 1/30/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition

-



This article was previously published under Q303018



SUMMARY
This article demonstrates how to automate Excel to create a command bar that contains buttons, drop-down list boxes, combo boxes, and pop-up menus.



MORE INFORMATION
The Visual C# .NET application catches and responds to the Click and Change events that the various command bar controls fire. Although this sample uses Excel as the host application, the command bar code will work in each of the Office applications.

Create the C# .NET Automation Client
 Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Under Project types click Visual C# Projects, then click Windows Application under Templates. Form1 is created by default. Add a reference to the Microsoft Excel Object Library and the Microsoft Office Object Library. To do this, follow these steps:  On the Project menu, click Add Reference. On the COM tab, locate the Microsoft Excel Object Library and click Select.

Note: Microsoft Office 2003 includes Primary Interop Assemblies (PIAs). Microsoft Office XP does not include PIAs, but they may be downloaded. For additional information about Office XP PIAs, click the article number below to view the article in the Microsoft Knowledge Base:

328912 INFO: Microsoft Office XP PIAs Are Available for Download

 Click OK in the Add References dialog box to accept your selection.  On the View menu, click Toolbox to display the toolbox and add a button to Form1.</li>  Double-click Button1. The code window opens at the onClick event for Button1. Add the following to the top of Form1.cs: using Office = Microsoft.Office.Core; using Excel = Microsoft.Office.Interop.Excel; </li>  In the code window, replace the following code private void button1_Click(object sender, System.EventArgs e) { } with: // Declare variables. Office.CommandBarButton oButton; Office.CommandBarComboBox oEdit; Office.CommandBarComboBox oDrop; Office.CommandBarComboBox oCombo; Office.CommandBarButton oPopupButton;

private void button1_Click(object sender, System.EventArgs e) { // Declare variables. Excel.Application oExcel; Office.CommandBar oCommandBar; Office.CommandBarPopup oPopup; Object oMissing = System.Reflection.Missing.Value;

// Start Excel. oExcel = new Excel.Application; // Show Excel and set UserControl oExcel.Visible = true; oExcel.UserControl = true; // Add a new workbook oExcel.Workbooks.Add(oMissing);

// Create a new command bar. oCommandBar = oExcel.CommandBars.Add(&quot;Billiards Sample&quot;,oMissing,oMissing,

// Add a button to the command bar. oButton = (Office.CommandBarButton)oCommandBar.Controls.Add(       Office.MsoControlType.msoControlButton,oMissing,oMissing,oMissing,oMissing); // Set the caption and face ID. oButton.Caption = &quot;New game&quot;; oButton.FaceId = 1845; // Set up a delegate for the Click event. Office._CommandBarButtonEvents_ClickEventHandler oButtonHandler = new Office._CommandBarButtonEvents_ClickEventHandler(oButton_Click); oButton.Click += oButtonHandler;

// Add an edit box to the command bar. oEdit = (Office.CommandBarComboBox) oCommandBar.Controls.Add(       Office.MsoControlType.msoControlEdit,oMissing,oMissing,oMissing,oMissing); // Show a vertical separator. oEdit.BeginGroup = true; // Clear the text and show a caption. oEdit.Text = &quot;&quot;; oEdit.Caption = &quot;Enter your name:&quot;; oEdit.Style = Office.MsoComboStyle.msoComboLabel; // Set up a delegate for the Change event. Office._CommandBarComboBoxEvents_ChangeEventHandler oEditHandler = new Office._CommandBarComboBoxEvents_ChangeEventHandler(oEdit_Change); oEdit.Change += oEditHandler;

// Add a combo box to the command bar. oCombo = (Office.CommandBarComboBox) oCommandBar.Controls.Add(       Office.MsoControlType.msoControlComboBox,oMissing,oMissing,oMissing,oMissing); // Add items to the combo box. oCombo.AddItem(&quot;Sharky&quot;,oMissing); oCombo.AddItem(&quot;Cash&quot;,oMissing); oCombo.AddItem(&quot;Lucky&quot;,oMissing); // Set the caption and style. oCombo.Caption = &quot;Choose your opponent:&quot;; oCombo.Style = Office.MsoComboStyle.msoComboLabel; // Set up a delegate for the Change event.

Office._CommandBarComboBoxEvents_ChangeEventHandler oComboHandler = new Office._CommandBarComboBoxEvents_ChangeEventHandler(oCombo_Change); oCombo.Change += oComboHandler;

// Add a drop-down list box to the command bar. oDrop = (Office.CommandBarComboBox) oCommandBar.Controls.Add(           Office.MsoControlType.msoControlDropdown,oMissing,oMissing,oMissing,oMissing); // Add items to the list box. oDrop.AddItem(&quot;8 Ball&quot;,oMissing); oDrop.AddItem(&quot;9 Ball&quot;,oMissing); oDrop.AddItem(&quot;Straight Pool&quot;,oMissing); oDrop.AddItem(&quot;Bowlliards&quot;,oMissing); oDrop.AddItem(&quot;Snooker&quot;,oMissing); // Set the value to the first in the list. oDrop.ListIndex = 1; // Set the caption and style. oDrop.Caption = &quot;Choose your game:&quot;; oDrop.Style = Office.MsoComboStyle.msoComboLabel; // Set up a delegate for the Change event. Office._CommandBarComboBoxEvents_ChangeEventHandler oDropHandler = new Office._CommandBarComboBoxEvents_ChangeEventHandler(oDrop_Change); oDrop.Change += oDropHandler;

// Add a pop-up menu to the command bar. oPopup = (Office.CommandBarPopup) oCommandBar.Controls.Add(           Office.MsoControlType.msoControlPopup,oMissing,oMissing,oMissing,oMissing); // Add a separator before the pop-up button. oPopup.BeginGroup = true; // Set the caption. oPopup.Caption = &quot;Rack 'em Up!&quot;; // Add a button to the pop-up. oPopupButton = (Office.CommandBarButton) oPopup.CommandBar.Controls.Add(           Office.MsoControlType.msoControlButton,oMissing,oMissing,oMissing,oMissing); // Change the face ID and caption for the button. oPopupButton.FaceId = 643; oPopupButton.Caption = &quot;Break!&quot;; // Set up a delegate for the Click event. Office._CommandBarButtonEvents_ClickEventHandler oPopupButtonHandler = new Office._CommandBarButtonEvents_ClickEventHandler(oPopupButton_Click); oPopupButton.Click += oPopupButtonHandler;

// Show the command bar to the user. oCommandBar.Visible = true; }

private void oButton_Click(Office.CommandBarButton Ctrl, ref bool Cancel) {   // Reset all values. oEdit.Text = &quot;&quot;; oDrop.ListIndex = 1; oCombo.Text = &quot;&quot;; Console.WriteLine(&quot;New game button clicked&quot;); }

private void oEdit_Change(Office.CommandBarComboBox Ctrl) {   Console.WriteLine(&quot;oEdit_Change event fired -- Player's name = &quot; + Ctrl.Text); }

private void oCombo_Change(Office.CommandBarComboBox Ctrl) {   Console.WriteLine(&quot;oCombo_Change event fired -- New opponent = &quot; + Ctrl.Text); }

private void oDrop_Change(Office.CommandBarComboBox Ctrl) {   Console.WriteLine(&quot;oDrop_Change event fired -- Game type = &quot; + Ctrl.Text); }

private void oPopupButton_Click(Office.CommandBarButton Ctrl, ref bool Cancel) {   System.Random oRand;

Console.WriteLine(&quot;oPopupButton_Click event fired&quot;); // Create a new random number class. oRand = new System.Random; String sWinner; // Get a random number and check its range. if (oRand.NextDouble > 0.5) sWinner = oEdit.Text; else sWinner = oCombo.Text; // Show a message box to the user. MessageBox.Show(&quot;Game: &quot; + oDrop.Text + &quot;\r\n\r\nName: &quot; + oEdit.Text +        &quot;\r\nOpponent: &quot; + oCombo.Text + &quot;\r\n\r\nWinner: &quot; + sWinner,&quot;Game Results&quot;); }

</li> Press F5 to build and run the program.</li> Click Button1 to start Excel, insert a new command bar, and insert controls on that command bar.</li></ol>

Additional Notes for Office XP
Office XP applications have a security option to allow programmatic access to the Visual Basic for Applications (VBA) object model. If this setting is &quot;off&quot; (the default), you may receive an error when you run the sample code. For additional information on this setting and how to correct the error, click the article number below to view the article in the Microsoft Knowledge Base:

282830 PRB: Programmatic Access to Office XP VBA Project Is Denied

<div class="references_section">