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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值