Thursday, May 21, 2009

Extending Visual Studio using Macros (VBA)

Like most every Microsoft Office product, Visual Studio (all versions including 2008) have support for using Macros to end the IDE. I had trouble finding the docs for where to access Macros in Visual Studio 2008. It is there, but at least for me it was hidden. Here is where you can find it (once you are in Visual Studio 2008)

Macro Explorer - View menu | Other Windows | Macro Explorer

The Macro menu is no longer in the Tools menu. However, you can add it there or anywhere else (such as a new menu or toolbar) you like by going to Tools menu | Customize... From here you will see a list of categories. Choose Tools from the list. The list of Commands to the right has many commands in it. The ones that you may want to grab are the following:

  • Cancel Recording
  • Edit Macro
  • Load Macro Project
  • Macro Explorer (same as the one in the View menu)
  • Macros IDE…
  • New Macro Command
  • New Macro Project
  • Pause/Resume Recording
  • Record Temporary Macro
  • Run Macro
  • Run Temporary Macro
  • Save Temporary Macro
  • Set Recording Project
  • Start Recording Temporary Macro
  • Stop Recording Temporary Macro
  • Unload Macro Project

Simply drag and drop any of the above items to a toolbar or menu. It is really pretty cool. When the customize window is open you can right-click and modify just about any menu item or toolbar item.

If you don’t want to mess with all that you can just type Control-Shift-R to start recording a temporary macro. This will cause the Macro toolbar to show. You can then use it to cancel, pause, and stop recording.

You can use the Macro Explorer to see the Temporary Macro under RecordingModule. You can Run, Edit, or Rename, or Delete the macro by right-clicking it. If you want to not have it rewritten the next time you record a temporary macro, you will need to rename it.

Once you have the Macro you like, you can add it to a toolbar (you can create a new one if you prefer) by going to Tools menu | Customize… | Macros category and then dragging your Macro from the Customize window to a toolbar.

Here is a sample macro that I recorded and then tweaked to go to the already open SQL file or open it first if it is not already open. See here for details.

References: How to Record Macros in Visual Studio 2005 or How to Record Macros in Visual Studio 2008

No comments: