How to create, use, and manage macros in Visual Studio

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:
    DTE.Windows.Item("Module1.vb").Activate()
    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 TODOWriter.
  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.

你可能感兴趣的:(create)