This documentation is archived and is not being maintained.

The Macro Project Object Model

Visual Studio .NET 2003

Visual Studio .NET includes tools for writing and recording macros. (For an overview, see Automating Repetitive Actions by Using Macros.) Visual Studio also includes a programmable object model that exposes macro projects. This model contains both general extensibility objects for language-neutral project items and macro-specific objects. The macro-specific objects, found in the VSLangProj namespace, are used just as in Visual Basic and Visual C# projects. This topic discusses:

  • Accessing the extensibility objects of a macro project
  • Macro projects and the VSLangProj.VSProject objects
  • Macro-specific elements of the VSLangProj objects

For more information on VSLangProj objects, see Introduction to Project Extensibility in Visual Basic and Visual C#. For more information on general extensibility, see Extending the Visual Studio Environment.

Accessing the Extensibility Objects of a Macro Project

Macro projects are accessed through the MacrosIDE property of the DTE extensibility object. The MacrosIDE property returns an object of type DTE, so that you have access to the Macros integrated development environment (IDE), including solutions, projects, tool windows, documents, and events. For more information, see the MacrosIDE property and the DTE object. The following macro lists all the modules in a macro project.

' Macro editor
Public Sub FindMacros()
   Dim macroproject As Project
   ' Retrieve the first project in the Macros IDE
   macroproject = DTE.MacrosIDE.Solution.Projects.Item(1)
   ' Display the project's name
   MsgBox(macroproject.Name)
   ' Display each project item (module).
   Dim projitem As ProjectItem
   For Each projitem In macroproject.ProjectItems
      MsgBox(projitem.Name)
   Next
End Sub 

Macro Projects and the VSLangProj Objects

In the general extensibility model, a project is represented by the generic Project object. The Project object has an Object property. In a macro project, the Object property returns an object of type VSProject. Since the type of the Object property is Object, you must cast the reference to type VSProject. You can use the PrjKind enumeration to check the project type before making the case. The cast would look like the following if done in a macro:

' Macro editor
Public Sub CastToVSProject()
   ' Retrieve the general extensibility object
   Dim macroproject As Project
   macroproject = DTE.MacrosIDE.Solution.Projects.Item(1)

   ' Cast for the macro specific information
   Dim vsproj As VSLangProj.VSProject

   ' Can test type of project before cast.
   If (macroproject.Kind = VSLangProj.PrjKind.prjKindVSAProject) Then
      vsproj = CType(macroproject.Object, VSLangProj.VSProject)
   End If
   MsgBox(vsproj.Project.Name)
End Sub

The VSProject object is the container for several other objects in the VSLangProj namespace, including References, Reference, Imports, and BuildManager objects.

In the general extensibility model, a project item is represented by the generic ProjectItem object. The ProjectItem object has an Object property. In a macro project, the Object property returns an object of type VSProjectItem. Since the type of the Object property is Object, you must cast the reference to type VSProjectItem. The cast would look like the following if done in a macro:

' Macro editor
Public Sub CastToVSProjectItem()
   Dim projitem As ProjectItem
   Dim vsitem As VSLangProj.VSProjectItem
   For Each projitem In _
      DTE.MacrosIDE.Solution.Projects.Item(1).ProjectItems
      vsitem = CType(projitem.Object, VSLangProj.VSProjectItem)
      MsgBox(vsitem.ProjectItem.Name)
   Next
End Sub

Macro-specific Elements of the VSLangProj Objects

The objects in the VSLangProj namespace are used for Visual Basic, Visual C#, and macro projects. There are some enumeration values that are particular to macro projects. The PrjKind enumeration has a value for macro projects, prjKindVSAProject. The PrjBrowseObjectCATID enumeration has values for extending the items in the Properties window:

  • prjCATIDVSAFolderBrowseObject
  • prjCATIDVSAFileBrowseObject
  • prjCATIDVSAConfig
  • prjCATIDVSAReferenceBrowseObject

Macro-specific Behavior of the VSLangProj Objects

The behavior of the VSLangProj objects varies slightly among Visual Basic, Visual C#, and macro projects. This is so that the object accurately represents the project type. Many of the properties and methods of the objects are not supported for macro projects, as listed below.

VSProject   This object represents the macro specific details of a project.

  • BuildManager Property This property is not supported and accessing this property throws an exception.
  • CopyProject Method This method is not supported and calling this method throws an exception.
  • WorkOffline Property This property always returns False. Trying to set this property does not throw an exception, but the property cannot be set to True.

VSProjectItem   This object represents the macro specific details of a project item.

Project Properties   These properties describe Visual Basic, Visual C#, and macro projects. For more information, see Properties Property (Visual Basic and Visual C# Project Object). The following properties are not supported:

ProjectItem Properties   These properties describe Visual Basic, Visual C#, and macro project items. For more information, see Properties Property (Visual Basic and C# ProjectItem Object). The following file properties are not supported and accessing them will throw an error:

The following folder properties are not supported and accessing them will throw an error.

Configuration Properties   These properties describe Visual Basic, Visual C#, and macro deployment configurations. For more information, see Properties Property (Visual Basic and Visual C# Configuration Object). The following file properties are not supported and accessing them will throw an error:

Reference   This object represents one project reference. For more information, see Reference Object. The following property is not supported and accessing this property will throw an exception:

References   This object represents a collection of all the references in the macro project. For more information, see References Object. The following methods are not supported, and calling one of them will throw an exception:

See Also

Introduction to Project Extensibility in Visual Basic and Visual C# | MacrosIDE Property | VSProject Object | References Object | Reference Object | Imports Object | VSProjectItem Object | BuildManager Object

Show: