The normal way to create VS macros is similar to the way VBA programs were created in Office programs. Although it's possible to start with a blank screen and just write the macro, it's often easier to use Visual Studio's Record TemporaryMacro function to get a "starter" VS macro that is fairly close to what you want in the final product by stepping through the actions you want in the macro while Visual Studio records them. (TemporaryMacro is a specific file that is a key part of the process as we'll soon see.)
There are some things about the way VS macros are managed that can be confusing. To get a foundation for discussion, however, let's record a really simple macro and run it. The confusing things about VS macros will be explained while we complete this simple macro.
The macro we will create will simply turn line numbers on and off in the Visual Studio edit window. You can do this pretty easily in Visual Studio by using Tools > Options... and then selecting Text Editor > Basic > Editor in the selection tree. Click the Line Numbers checkbox in the Interaction group and click the OK button. (This is different in Visual Studio.NET 2005!)
You might want a little more convenient way to do this (or a way to do it without having to remember exactly where this option is in Visual Studio). If you check through the Customize options for the toolbar in Visual Studio, you will see that you can't even bring up the Options dialog anymore. (In VS 2005, you used to be able to display it, but you couldn't create a button that would change any specific option.)
A VS Macro that you could assign to a button on the toolbar is the answer. (Experienced readers may know that this macro is actually included in the VS Macros "Samples". That's OK ... Creating it again will make the concepts clear.)
Here's a quick summary of the steps to create the macro:
- Start the Macro Recorder (Tools > Macros > Record TemporaryMacro). A small dialog window pops up to show that the recorder is running.
- Select Tools > Options... and then select Text Editor > Basic > Editor in the selection tree. Click the Line Numbers checkbox and click the OK button. If there is a program in the code window, you will also see line numbers displayed.
- Click the Stop Recording button in the dialog that popped up earlier.
- Display the Macro Explorer in Visual Studio (Tools > Macros > Macro Explorer).
- Right-click TemporaryMacro and select Edit.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
A new application, the Visual Studio Macros IDE will open now. One of the things that can get you twisted up is confusing Visual Studio.NET itself with this new IDE. Working with the VS macros IDE is similar to working in Visual Studio .NET, but it's not the same. You have to use the two together to do everything.
For example, you can't create a new VS macro Project in the VS macro IDE. Notice that there is no "New" menu option under the "File" menu. Instead, if you want to create one, you have to do it in Visual Studio by selecting New Macro Project (either under "Tools" or in the "Macro Explorer"). And even then, there's no way to actually delete the project in Visual Studio. (You have to delete it from the VSMacros80 folder in Windows after unloading it.)
You can only access the macros by using menu commands in Visual Studio. If you simply copy files into the folder, they still won't show up in Visual Studio. And if you double click the vsmacros file, you only get a notification telling you that you can't open the file that way.
Another concept in creating macros is the RecordingModule macro module. This is an automatically created module where Visual Studio places recorded macros. If you recorded a macro earlier and haven't saved it yet, a new recorded macro will overwrite the old one. This is different from the way VBA macros are recorded in Office programs like Excel or Word.
While we're discussing the detailed commands, I should warn you that the instructions for creating and running Visual Studio macros at MSDN seemed to have a few errors. (They have been there for two years now without being corrected.) For Example, MSDN states:
"To record a macro
Create a new text file by choosing File on the New submenu on the File menu."
There isn't one in VS 2005 or 2008. These instructions are for VS 2002/2003. And the "How to: Manage Macros" bulleted link is listed twice. Somebody is asleep at the switch.
Similar problems where the MSDN instructions just don't work are found throughout the MSDN Visual Studio Macros documentation. For example, most of the Visual Studio Macro shortcuts like Alt-11 or Ctrl-Shift-P have been dropped in the latest version of Visual Studio. Hopefully, Microsoft will get this documentation updated soon.
The Edit window of the VS macros IDE shows what has been recorded so far by the Visual Studio Macro Recorder:
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
An examination of the code shows that there's a problem. Even though you selected the option to turn on line numbers, it wasn't captured in the macro code. Only the Tools > Options dialog is displayed by the macro. You will discover that failing to capture all of the Visual Studio actions is a common problem. When this happens, Microsoft notes that, "you will have a recording gap in your macro, and thus, the macro will not work as expected. If this happens, you can manually edit the macro."
Change what? (I hear you asking.) Visual Studio provides a complete automation object model, called the DTE object, to help you do that. Although the code that was captured gets you part of the way, it doesn't really help you change the line numbers option. The next page goes into detail about that!
