Ver versión en español    
 
Home
10 Reasons to use MZ-Tools
MZ-Tools 6.0 for VS.NET
Editions
Features
Online Documentation
MZ-Tools SDK
Download
Purchase
Version History (RSS)  
FAQ & Support
MZ-Tools 3.0 for VB6 & VBA
Features
Online Documentation
Download (freeware)
Donations (Paypal)
Version History (RSS)  
FAQ & Support
User Reviews
Community Place
Contact  
For Add-In Developers
About
   
User Testimonials

I'm an avid supporter of MZ-Tools. It's a product I couldn't do without and your level of support is outstanding.

Jan Hyde (Visual Basic MVP)

You will soon wonder how you ever lived without it.

Andy Maggs

More user reviews
 
HOWTO: Retrieve the Command that created a CommandBarControl from a Visual Studio add-in or macro

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: August 2008   Microsoft Visual Studio .NET 2003
Updated: March 2010   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
Introduction

While most of the time an add-in creates a CommandBarControl from a Command using the Command.AddControl method and therefore it can hold the information that matches them, sometimes it can be needed to retrieve the Command that created a given CommandBarControl without further information.

More Information

The CommandBarControl class lacks a Command property because Visual Studio borrowed the commandbar model from Microsoft Office, which doesn't have the notion of commands (at least externally to the model).

To suply this lack, the automation model (EnvDTE) of Visual Studio provides the EnvDTE.Commands.CommandInfo method, that receives as input a CommandBarControl and have two output parameters to return the Guid and Id of the underlying EnvDTE.Command (commands are actually identified by Guid and Id, while the Name property may not be unique). With the Guid and Id of the command you can call the EnvDTE.Commands.Item method that can receive both values (rather than a single index) and returns the EnvDTE.Command.

The following macro shows the name of the underlying command of the first button on the "Tools" commandbar:

Sub GetCommandFromCommandBarControl()

   Dim iID As Integer
   Dim sGUID As String
   Dim objCommand As Command
   Dim objCommandBarControl As Microsoft.Office.Core.CommandBarControl

   objCommandBarControl = DTE.CommandBars.Item("Tools").Controls(1)

   DTE.Commands.CommandInfo(objCommandBarControl, sGUID, iID)

   objCommand = DTE.Commands.Item(sGUID, iID)
   
   System.Windows.Forms.MessageBox.Show(objCommand.Name)

End Sub

Related articles


Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this


   Top