This documentation is archived and is not being maintained.

AddControl Method

Visual Studio .NET 2003

Creates a persistent command bar control for the command.

[Visual Basic .NET]
Public Function AddControl( _
   ByVal Owner As Object, _
   Optional ByVal Position As Integer = 1 _
) As CommandBarControl
[Visual Basic 6]
Function AddControl( _
   ByVal Owner As Object, _
   Optional ByVal Position As Long = 1 _
) As CommandBarControl
HRESULT __stdcall AddControl(
   IDispatch* Owner,
   long Position,
   /* [out, retval] */ CommandBarControl** retVal
public CommandBarControl AddControl(
   object Owner,
   int Position
[JScript .NET]
public function AddControl(
   Owner : Object,
   Position : int
) : CommandBarControl


Required. An Office CommandBar object to which the new command bar control is to be added.
Optional. The index position, starting at one, in the command bar to place the new control.


The environment saves the control and its placement, making it available according to the command's ContextUIGUIDs or its responses to QueryStatus Method during each new session of the environment, regardless of whether the Add-in is loaded.


Imports Microsoft.Office.Core
Sub AddControlExample()
   ' Before running, you must add a reference to the Office 
   ' typelib to gain access to the CommandBar object. Also, for this    
   ' example to work correctly, there should be an add-in available 
   ' in the Visual Studio .NET environment.
   Dim cmds As Commands
   Dim cmdobj As Command
   Dim customin, customout As Object
   Dim cmdbarobj As CommandBar
   Dim colAddins As AddIns

   ' Set references.
   colAddins = DTE.AddIns()
   cmds = DTE.Commands
   cmdobj = cmds.Item("File.NewFile")

   ' Create a toolbar and add the File.NewFile command to it.
   cmdbarobj = cmds.AddCommandBar("MyCmdBar", vsCommandBarType.vsCommandBarTypeToolbar)
   MsgBox("Commandbar name: " & cmdbarobj.Name)
   cmds.AddNamedCommand(colAddins.Item(1), "MyCommand", "Button Text", "Some tooltip", True)
End Sub

See Also

Applies To: Command Object