Microsoft KB Archive/303017

= How To Use Automation to Create Office Command Bars and Controls with Visual Basic .NET =

Article ID: 303017

Article Last Modified on 8/23/2005

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition

-



This article was previously published under Q303017



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 Basic .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 Visual Basic .NET Automation Client
 Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Under Project types click Visual Basic 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 theMicrosoft 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 selections.  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.vb: Imports Office = Microsoft.Office.Core Imports Excel = Microsoft.Office.Interop.Excel </li>  In the code window, replace the following code Private Sub Button1_Click(ByVal sender As System.Object, _                         ByVal e As System.EventArgs) _ Handles Button1.Click End Sub with: ' Declare variables. Dim WithEvents oButton As Office.CommandBarButton Dim WithEvents oEdit As Office.CommandBarComboBox Dim WithEvents oDrop As Office.CommandBarComboBox Dim WithEvents oCombo As Office.CommandBarComboBox Dim WithEvents oPopupButton As Office.CommandBarButton

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Declare variables. Dim oExcel As Excel.Application Dim oCommandBar As Office.CommandBar Dim oPopup As Office.CommandBarPopup

' Start Excel. oExcel = New Excel.Application ' Show Excel and set UserControl. oExcel.Visible = True oExcel.UserControl = True ' Add a new workbook. oExcel.Workbooks.Add

' Create a new command bar. oCommandBar = oExcel.CommandBars.Add(&quot;Billiards Sample&quot;)

' Add a button to the command bar. oButton = oCommandBar.Controls.Add(Office.MsoControlType.msoControlButton) ' Set the caption and face ID. oButton.Caption = &quot;New game&quot; oButton.FaceId = 1845

' Add an edit box to the command bar. oEdit = oCommandBar.Controls.Add(Office.MsoControlType.msoControlEdit) ' 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

' Add a combo box to the command bar. oCombo = oCommandBar.Controls.Add(Office.MsoControlType.msoControlComboBox) ' Add items to the combo box. oCombo.AddItem (&quot;Sharky&quot;) oCombo.AddItem (&quot;Cash&quot;) oCombo.AddItem (&quot;Lucky&quot;) ' Set the caption and style. oCombo.Caption = &quot;Choose your opponent:&quot; oCombo.Style = Office.MsoComboStyle.msoComboLabel

' Add a drop-down list box to the command bar. oDrop = oCommandBar.Controls.Add(Office.MsoControlType.msoControlDropdown) ' Add items to the list box. oDrop.AddItem (&quot;8 Ball&quot;) oDrop.AddItem (&quot;9 Ball&quot;) oDrop.AddItem (&quot;Straight Pool&quot;) oDrop.AddItem (&quot;Bowlliards&quot;) oDrop.AddItem (&quot;Snooker&quot;) ' 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

' Add a pop-up menu to the command bar. oPopup = oCommandBar.Controls.Add(Office.MsoControlType.msoControlPopup) ' 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 = oPopup.CommandBar.Controls.Add(Office.MsoControlType.msoControlButton) ' Change the face ID and caption for the button. oPopupButton.FaceId = 643 oPopupButton.Caption = &quot;Break!&quot;

' Show the command bar to the user. oCommandBar.Visible = True End Sub

Private Sub oButton_Click(ByVal Ctrl As Office.CommandBarButton, _                         ByRef CancelDefault As Boolean) Handles oButton.Click ' Reset all values. oEdit.Text = &quot;&quot; oDrop.ListIndex = 1 oCombo.Text = &quot;&quot; Console.WriteLine (&quot;New game button clicked&quot;) End Sub

Private Sub oCombo_Change(ByVal Ctrl As Office.CommandBarComboBox) Handles oCombo.Change Console.WriteLine (&quot;oCombo_Change event fired -- New opponent = &quot; + Ctrl.Text) End Sub

Private Sub oDrop_Change(ByVal Ctrl As Office.CommandBarComboBox) Handles oDrop.Change Console.WriteLine (&quot;oDrop_Change event fired -- Game type = &quot; + Ctrl.Text) End Sub

Private Sub oEdit_Change(ByVal Ctrl As Office.CommandBarComboBox) Handles oEdit.Change Console.WriteLine (&quot;oEdit_Change event fired -- Player's name = &quot; + Ctrl.Text) End Sub

Private Sub oPopupButton_Click(ByVal Ctrl As Office.CommandBarButton, _                              ByRef CancelDefault As Boolean) Handles oPopupButton.Click Dim oRand As System.Random

Console.WriteLine (&quot;oPopupButton_Click event fired&quot;) ' Create a new random number class. oRand = New System.Random Dim sWinner As String ' Get a random number and check its range. If (oRand.NextDouble > 0.5) Then sWinner = oEdit.Text Else sWinner = oCombo.Text End If   ' Show a message box to the user. MessageBox.Show(&quot;Game: &quot; + oDrop.Text + vbCrLf + vbCrLf + &quot;Name: &quot; + oEdit.Text + _           vbCrLf + &quot;Opponent: &quot; + oCombo.Text + _            vbCrLf + vbCrLf + &quot;Winner: &quot; + sWinner, &quot;Game Results&quot;) End Sub </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">