Microsoft KB Archive/216548

= How To Add Submenus Using Add-Ins =

Article ID: 216548

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q216548



SUMMARY
This article demonstrates how to add submenus to both the Visual Basic IDE and the currently active form of the project using an Add-in.



MORE INFORMATION
The following steps explain how to write an Add-in that adds submenus to the Visual Basic IDE:  Start a new Visual Basic Add-in project. A frmAddIn form and Connect AddinDesigner (Class) are created by default.  Open the Connect code window and locate the function AddToAddInCommandBar. Replace the existing code with the following:

Function AddToAddInCommandBar(sCaption As String) Dim cbMenuCommandBar   As Office.CommandBarPopup  ' cmd bar object Dim cbSubMenuItem      As Office.CommandBarButton ' Added Dim cbMenu             As Object On Error GoTo AddToAddInCommandBarErr

' see if we can find the Add-Ins menu Set cbMenu = VBInstance.CommandBars("Add-Ins") If cbMenu Is Nothing Then ' not available so you fail Exit Function End If

' add it to the command bar, Next line Added Set cbMenuCommandBar = cbMenu.Controls.Add(msoControlPopup,, , 1) ' set the caption cbMenuCommandBar.Caption = sCaption ' Next line Added Set cbSubMenuItem = cbMenuCommandBar.Controls.Add(msoControlButton) cbSubMenuItem.Caption = "One" ' Added

Set AddToAddInCommandBar = cbSubMenuItem ' Added Exit Function

AddToAddInCommandBarErr: End Function  Run the project. Start another instance of the Visual Basic IDE creating a new Standard EXE project. Form1 is created by default. Click the Add-In menu. It contains "My AddIn," which in turn contains one submenu named "One."

The following code can be used to create an Add-in to automate the process of adding submenus to the active form:  Start a new Visual Basic Add-in project. A frmAddIn form and Connect AddinDesigner (Class) are created by default.</li> Open frmAddIn in design mode and place a CommandButton on it. Command1 is created by default.</li>  Add the following code to the General Declarations section of Form1:

Private Sub Command1_Click Dim CurrentForm As VBComponent     ' current form Dim Ctls       As VBControls       ' form's controls Dim Parent     As VBControl Dim Child      As VBControl Dim GrandChild As VBControl

Set CurrentForm = VBInstance.SelectedVBComponent Set Ctls = CurrentForm.Designer.VBControls Set Parent = Ctls.Add("VB.Menu") Parent.Properties!Caption = "&Parent" Set Child = Parent.ContainedVBControls.Add("VB.Menu", Parent) Child.Properties!Caption = "&Child" Set GrandChild = Child.ContainedVBControls.Add("VB.Menu", Child) GrandChild.Properties!Caption = "&Grand Child" End Sub </li> Run this project.</li> Start another instance of the Visual Basic IDE creating a new Standard EXE project. Form1 is created by default.</li> Click the Add-In menu and select "My AddIn."</li> Click the Command1 button in the My Add In dialog.</li> A menu Parent will be added to Form1. The Parent contains submenu Child, which in turn contains a submenu GrandChild.</li></ol>

Additional query words: addin

Keywords: kbhowto kbmenu kbaddin KB216548

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.