This documentation is archived and is not being maintained.

Walkthru:Creating Macros 

Visual Studio 2005
Extensibility Guided Tour Send comments on this topic.
Walkthru:Creating Macros

Glossary Item Box

Walkthrough: Creating Macros

Macros are the easiest way to automate and extend the Visual Studio IDE. With no prior knowledge of the IDE you can record actions as a macro and then run them as a single batch of commands. Each action is recorded as Visual Basic .NET code that can be edited and debugged in a special Macros IDE.

The following walkthrough shows you how to record and edit a macro that adds boilerplate comments to the top of all .cs files in a Visual Studio project.

1.  In Visual Studio 2005 create a new Visual C# Class Library project named "MacroWalkthrough".

2.  In Solution Explorer, right click on the MacroWalkthrough project and select the Add | Class menu option. In the Add New Item dialog, specify "foo.cs" as the name of your class. Repeat these steps and add a "bar.cs" class as well.

3.  Ensure that bar.cs is open in the editor.

4.  Select the Tools | Macros | Record TemporaryMacro menu command (or press Ctrl+Shift+R).

5.  Place your cursor in the top left corner of bar.cs and type "// This is a Guided Tour Walkthrough", and then press Enter. Type "// You could keep adding more comments", and then press Enter.

6.  In the Recorder window click the Stop Recording icon.

7.  Save and close bar.cs.

8.  Select the Tools | Macros | Macro Explorer menu command (or press Alt+F8).

9.  Right-click TemporaryMacro and select Run.

The comments are added to foo.cs - a different file than the one where the changes were comments were initially inserted.

10.  In the Macro Explorer, right-click TemporaryMacro and select Edit.

Note how your IDE actions were converted to Visual Basic .NET code. For example, typing was recorded against the currently active document by Selection.Text property, and carriage returns were recorded with the NewLine() method.

11.  Select the existing code in the TemporaryMacro() Sub and press CTRL+X to cut, or remove the code to your clip board.

12.  Above or below the now empty TemporaryMacro() Sub, create a new Sub: AddComments(), and within it, paste the copied code by pressing CTRL+V. Once completed, the editor should look like the following image:

13.  Add the following subroutine by pasting it in either above or below the existing code in the current Recording Module:

Sub IterateFiles()
    Dim solution As Solution = DTE.Solution
    Dim project As Project = solution.Projects.Item(1)

    Dim file As ProjectItem

    For Each file In project.ProjectItems
        If file.Name.EndsWith(".cs") Then


        End If
End Sub

This code iterates through the current project, checks each file, and when it detects a file ending with ".cs" activates it, and then hands control of the Macro off to the AddComments() subroutine. It then saves and closes each file.

14.  In the TemporaryMacro() subroutine, add the following code:


15.  Save your changes by pressing CTRL+S. Switch to Visual Studio, and in the Macros Explorer right-click TemporaryMacro and select Run.

16.  Open and check each file. The comments you added to bar.cs have now been added to all three .cs files in the project.

In this walkthrough you learned how to record a macro that added comments to the top of a code file. You then opened the recorded macro in the Macros IDE and manually modified the Visual Basic .NET source code to extend the macro, allowing it to open all .cs files in a project, add the same boilerplate comments, and then save and close them.