Here's a quick tutorial on how to get to writing or entering macros in Visual Studio. 

Creating:

  1. Choose the “Tools.Macros.Macros Explorer...” menu item.
  2. If you've never used this capability before you should see a new toolwindow with a treeview in it.  There will be two nodes listed under the Macros root node, “MyMacros“ and “Samples“.
  3. Expand “MyMacros”, right click on “Module1“ and choose “Edit“.
  4. A new IDE will open that  appears astonishingly like the Visual Studio IDE you're used to.  It actually IS running much of the same code but it is limited in that it only supports writing, running and debugging macros.  Macros are currently written in VB (but with some finagling you can access the automation model using any language, if it doesn't need to be a macro).  The IDE supports everything you'd expect when doing VB development, namely Intellisense.  This feature allows even VB newbies to make quick and easy progress.
  5. Create from scratch or paste from the clipboard anything macro you'd like.

Running (several options)

  • Double click on any of the macros that appear in the Macros Explorer window.
  • Run the macro within the Macros IDE, like you would run any project.  Just be sure to have the system caret in the macro you want to run or debug before pressing 'Go'.
  • Run the macro from the command window in the main VS IDE.  For example:

>Macros.MyMacros.Module1.MyNiftyMacro

  • Add the macro to a menu, context menu or toolbar.  See below.
  • Bind the macro to a keystroke. See below.
  • Neat Trick: How many of you knew you could run a command from the 'Find' combobox that appears on the standard toolbar?   You can execute almost any command (menu item) or macro (as above) in this thing.  Just preface your command with the '>'.  For example, try this:

>File.OpenFile c:\windows\setuplog.txt

Why did anybody ever decide to implement this?  I really don't know the answer to that, but I do know it happened long ago and is something command-line addicts refuse to give up.   My theory is that it was the original software anomaly that coined the phrase 'That's not a bug, it's a feature!'  (just kidding... I think...)

Adding to Menus and Context Menus

  • To add a macro to any VS menu simply:
    • Choose Tools->Customize
    • In the "Commands" tab choose "Macros" in the Categories list.
    • Drag and drop the macro onto a toolbar or menu in the VS shell
  • Adding a macro to a context menu is not much more difficult (VS 2005 and later)
    • Choose Tools->Customize
    • Show the "Toolbars" tab in the customize dialog.
    • Check the "Context Menus" toolbar.  Notice that within the VS IDE you'll see a new "toolbar" added that lists all the context menus it knows about.
    • Now switch back to the "Commands" tab and drag and drop onto any of the context menus.

Binding a macro to a keystroke 

  • Choose Tools->Customize
  • Click the "Keyboard..." button at the bottom of the dialog 
  • In the "Show commands containing:" edit box, type "macros" or the partial name of the macro you're looking for.
  • Select the macro you're interested in.
  • Put the caret in the "Press shortcut keys:" edit box and press the keystroke combo you'd like to bind the macro to.
  • If that binding is already used, you'll see what it is in the "Shortcut currently used by:" window at the bottom of the dialog.

Note that by default the keystroke will be bound globally.  Some keystroke combinations can be made to be active only in certain contexts.  Peruse help for more information about this.