HOWTO: Get the programmable inner object of a toolwindow

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: February 2006   Microsoft Visual Studio .NET 2003
Updated: March 2013   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012

This article explains how to get the programmable inner object of a Visual Studio .NET toolwindow from an add-in or macro.

More Information

Some Visual Studio toolwindows can be automated using its inner object, which is detailed in the following table:

Window Programmable Object
Solution Explorer EnvDTE.UIHierarchy
Macro Explorer EnvDTE.UIHierarchy
ToolBox EnvDTE.ToolBox
Task List EnvDTE.TaskList
Error List EnvDTE80.ErrorList
Command Window EnvDTE.CommandWindow
Output Window EnvDTE.OutputWindow

Note: the Class View does not provide a programmable object.

There are several ways of getting the programmable object inside a toolwindow:

  • For all Visual Studio versions, first you can get the window from the DTE.Windows collection using the GUID of the window (hard-coded or using the ones defined in EnvDTE.Constants.vsext_wk_XXX, EnvDTE.WindowKinds.vsWindowKindXXX or in EnvDTE80.WindowKinds.vsWindowKindXXX). Then, you can get its Window.Object property:
Dim objWindow As EnvDTE.Window
Dim objToolBox As EnvDTE.ToolBox

objWindow = DTE.Windows.Item(EnvDTE.Constants.vsext_wk_Toolbox)
objToolBox = CType(objWindow.Object, EnvDTE.ToolBox)

To know the actual type returned by Window.Object, see HOWTO: Know the actual type behind a System.__ComObject type returned by an .Object property.

  • For Visual Studio 2005 and higher, you can use the EnvDTE80.DTE2.ToolWindows class, which provides some common programable objects and also the GetToolWindow method (which received the toolwindow GUID):
Dim objToolBox As EnvDTE.ToolBox

objToolBox = CType(DTE, DTE2).ToolWindows.ToolBox

objToolBox = CType(CType(DTE, DTE2).ToolWindows.GetToolWindow( _
   EnvDTE.Constants.vsext_wk_Toolbox), EnvDTE.ToolBox)

Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)