Article ID: 555391 - Last Review: August 10, 2005 - Revision: 1.0

HOWTO: Automating Visual Studio .NET from outside the IDE

Author: Carlos Quintero MVP
Expand all | Collapse all

SUMMARY

This article describes how to automate the Visual Studio .NET IDE from outside the IDE.

SYMPTOMS

This article describes how to automate the Visual Studio .NET IDE from outside the IDE.

MORE INFORMATION

Visual Studio .NET exposes an extensibility model that can be used to automate it. This object model resides in the EnvDTE.dll assembly, which you can explore using the Object Browser. The root class of the object model is EnvDTE.DTE, and therefore you need an instance of this class to automate it.
 
You have the following ways to automate the Visual Studio .NET IDE:
 
- Using macros: macros are procedures written in VB.NET that allow to automate some tasks from within the IDE. To write macros you use the Macros IDE (Tools, Macros, Macros IDE menu). To explore and run your macros you use the Macro Explorer window (View, Other Windows, Macro Explorer menu). Macros provide an implicit instance of the EnvDTE.DTE class named DTE.
 
- Using an add-in: an add-in is a compiled DLL that through a special registration it is loaded by Visual Studio .NET and provides new commands, command bars and buttons to perform new actions. Visual Studio .NET passes an instance of the EnvDTE.DTE class to a method of the add-in when the connection takes place.
 
The previous methods automate the IDE from within, but there is a third method that you can use to automate the IDE from the outside, for example from other application or from a script. To do this, first you need to create an instance of the EnvDTE.DTE class. This is done using the CreateObject function of most COM-aware languages, passing the ProgID of the class. The list of available ProgIDs is the following:
 
- For Visual Studio .NET 2002: "VisualStudio.DTE.7"
- For Visual Studio .NET 2003: "VisualStudio.DTE.7.1"
- For Visual Studio 2005: "VisualStudio.DTE.8.0"
 
For the highest installed version of Visual Studio .NET, you would use the version-independent ProgID "VisualStudio.DTE".
 
There are 2 important properties that control the behavior of the IDE while you are automating it from the outside:
 
- DTE.MainWindow.Visible: by default, when you create an instance of the IDE, it is invisible. If you want to make it visible, you must set this property to True.
 
- DTE.UserControl: when set to True, the IDE remains open after you are done with the automation. This is useful if you want to open the IDE, perform some action, and keep if open for the user to continue using it. When set to False, the object is released after you are done with the automation.
 
The following VBScript sample shows how to create and instance of VS.NET 2003 and show its name and version:
 
 
Dim objDTE
 
' Creates an instance of the VS.NET 2003 IDE
Set objDTE = CreateObject("VisualStudio.DTE.7.1")
 
' While the instance is still invisible, show its name and version
MsgBox objDTE.Name & " " & objDTE.Version
 
' Make it visible and keep it open after we finish this script
objDTE.MainWindow.Visible = True
objDTE.UserControl = True
 
 
References:
 
- Manipulating the Development Environment: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxoriManipulatingDevelopmentEnvironment.asp
 
- Automation and Extensibility Reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxoriExtensibilityReference.asp

 

APPLIES TO
  • Microsoft Visual Studio .NET 2002 Enterprise Developer
  • Microsoft Visual Studio .NET 2002 Professional Edition
  • Microsoft Visual Studio .NET 2003 Enterprise Architect
  • Microsoft Visual Studio .NET 2003 Enterprise Developer
  • Microsoft Visual Studio .NET 2003 Professional Edition
Keywords: 
kbpubmvp kbpubtypecca kbhowto KB555391
Community Solutions ContentCOMMUNITY SOLUTIONS CONTENT DISCLAIMER
MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.
 

Article Translations