How to create, use, and manage macros in Visual Studio .NET or in Visual Studio 2005


This article demonstrates how to create a new macro project in Visual Basic .NET or in Visual Basic 2005, and then record a simple macro that inserts a TODO token into the code and subsequently opens the Task List window to view the TODO task. It also shows you how to map the macro to a keyboard shortcut.


You need the following hardware and software to perform the procedures in this article:
  • Microsoft Windows Server 2003, Microsoft Windows 2000 Professional (or Microsoft Windows 2000 Server) or Microsoft Windows XP Professional with the .NET Framework installed.
  • Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.

Create, Use, and Manage Macros

The following procedure illustrates how to create a new macro project and then record a simple macro that inserts a TODO token into the code and subsequently opens the Task List window to view the TODO task. The macro is also mapped to a keyboard shortcut.

To create, use, and manage macros, follow these steps:
  1. Click Start, point to Programs, point to Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, and then click Microsoft Visual Basic .NET or Microsoft Visual Basic 2005.
  2. Click New Project.
  3. In the Project Types list, click Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  4. In the Templates list, click Console Application.

    It does not matter which type of application you choose to demonstrate the creation and use of the following sample macro. A console application is appropriate here because it is simple and lightweight.
  5. Click OK.

    Module1.vb should now be open in the Code Editor window.
  6. To illustrate how macros are managed, click Tools, point to Macros, and then click Macro Explorer to open Macro Explorer (or press ALT+F8).

    This is the first step to create a new macro with the Visual Studio .NET or Visual Studio 2005 macro recording feature.

    The MyMacros project should be visible. It is installed by default in the VSMacros folder beneath the Visual Studio projects location, as set in the Projects and Solutions folder (click Tools, point to Options, and then click Environment).

    NOTE: To manage (and organize) macros, create new macro projects.
  7. In the Macro Explorer, right-click Macros, and then click New Macro Project.
  8. In the New Macro Project dialog box, type KeyboardUtil in the Name box, and then click Open.

    Note In Visual Studio 2005, click Add instead of Open.
  9. In Macro Explorer, right-click KeyboardUtil, and then set this as Recording Project.

    Newly recorded macros will now be placed in this new macro project.
  10. The macro that you will create inserts the TODO token into your code and then opens the Task List window with all tasks in view. This token is automatically parsed by the integrated development environment (IDE) to aid developers in marking up their code.

    NOTE: A full list of tokens and Task List options are available when you click Tools, point to Options, point to to Environment, and then click Task List.
  11. Press CTRL+SHIFT+R to start recording the macro.

    A small Recorder toolbar should appear. All interactions with the keyboard and Visual Studio .NET interface are now recorded.
  12. In the Code Editor window, type
    TODO: inside the Sub Main() frame in the code.
  13. Click View, point to Show Tasks, and then click All.
  14. Press CTRL+SHIFT+R to stop recording.
  15. In Macro Explorer, notice that a new file called RecordingModule is now displayed, with TemporaryMacro underneath it. Right-click RecordingModule, and then click Edit to open the Macros IDE and view the code that was generated when you recorded your actions in Visual Studio .NET or in Visual Studio 2005.

    Here you can edit the code directly, or write macros from scratch.
    NOTE: When you view this code, check it against the code included in the "Complete Code Listing" section of this article. The macro-recording process records all clicks and key presses that you make. Therefore, if you make an extra mouse click, this will be recorded. The resulting extra line will most likely be as follows:
    This line moves the Active Window to Module1.vb. If you want this macro to work in whatever window is currently active, remove this line.
  16. Close the macro IDE.
  17. The macro is not saved until you rename it. In Macro Explorer, right-click TemporaryMacro, and then click Rename. Rename the macro as
  18. Although you can run the macro directly from Macro Explorer, it is most convenient to associate it with a keyboard shortcut. To do this, click Tools, point to Options, point to Environment, and then click Keyboard.
  19. In the Show commands containing box, type the following: todo.

    The macro that you just created should appear in the list immediately underneath.
  20. Place the mouse pointer in the Press shortcut keys box, and then press CTRL+SHIFT+RIGHT ARROW.

    The shortcut currently used by this field should alert you to the fact that this keyboard combination is already in use for the Edit.SizeControlRight command.
  21. Press BACKSPACE to delete the key combination. Press CTRL+SHIFT+BACKSPACE. This combination is not in use; therefore, click Assign.
  22. When prompted, click Yes, and then type any name for the new keyboard scheme, which is a copy of the default scheme with the new macro mapping. Click OK twice.

Complete Code Listing (RecordingModule)

Option Strict Off
Option Explicit Off
Imports EnvDTE
Imports System.Diagnostics

Public Module RecordingModule
Sub TODOWriter()
DTE.ActiveDocument.Selection.Text = "' TODO:"
End Sub
End Module


  1. Reset the IDE environment. To do this, click View, point to Show Tasks, and then click Build Errors.

    This ensures that the TODO tasks are not in view. Remove any code that you typed within the Sub Main() frame.
  2. With your mouse pointer positioned inside Sub Main(), press CTRL+SHIFT+BACKSPACE.

    A TODO token should be inserted, a new line created, and the TODO task should now be visible in the Task List window. To see how the TODO task works, type some task I need to perform later after TODO:, and then press DOWN ARROW to move your pointer to the next line and cause the task to appear.
    NOTE: When the macro is finished running, the Task List is the active window. If you press ESC, you return control to the Code Editor, where the TODO token was inserted by the macro.
  3. In Macro Explorer, double-click TODOWriter to run the macro. (Before you rename a newly recorded macro, you can also run it by pressing CTRL+SHIFT+P.)


As noted in the .NET Framework SDK documentation:
  • You cannot run a macro if its parent project cannot be built, such as when another macro in the project contains errors.

  • When you run a macro from Macro Explorer, the environment considers the last window that was opened immediately before you open Macro Explorer to be the last activated window, and therefore the macro runs as if that window has focus. This prevents problems with the Macro Explorer window inadvertently gaining the focus in your macro's operation.



ID do Artigo: 317347 - Última Revisão: 13 de mai de 2008 - Revisão: 1