Creating and Using a Macro
Keyboard: CTRL + SHIFT + R (record/stop recording); CTRL + SHIFT + P (run)
Menu: Tools -> Macros -> Record Temporary Macro; Tools -> Macros -> Run Temporary Macro; Tools -> Macros -> Save Temporary Macro
Command: Tools.RecordTemporaryMacro; Tools.RunTemporaryMacro; Tools.SaveTemporaryMacro
Versions: 2008,2010
Published: 12/2/2010
Code: vstipEnv0055
You can record macros to do just about anything in Visual Studio. In this example, we will create a macro that adds a new class to our project. First, create a new project. For this example create a Console Application:
Now we are going to add a class to the project and give the class a name. When we do this, we will record the actions into a temporary macro. Press CTRL + SHIFT + R to begin recording our macro. You will know you are recording if the status bar indicates it in the lower left-hand corner:
Add a new class (CTRL + SHIFT + A) called "Bubba.cs":
DANGER: There are lots of little "gotchas" that you will run into doing macros. One that took me a little while to figure out while doing this example was leaving off the ".cs" at the end of the file name. For some reason it doesn't like that at all. Keep an eye out for little things like that as you use this feature.
Now add a comment to the class:
Now stop recording (CTRL + SHIFT + R) then go to Tools -> Macros -> Save Temporary Macro. Name the macro "MakeBubba" in the Macro Explorer:
That's it. Now we can test out our new macro by creating a new project and going to the Macro Explorer (ALT + F8). Then RIGHT CLICK the MakeBubba macro and choose "Run":
It should make the new class and put your comment in:
Once you have created your macro you might want to see the code behind it, you can go to the Macro Explorer (ALT + F8) and RIGHT CLICK any macro then choose "Edit" to see the code. Here is what my code (cleaned up a bit) looks like for the macro we just made:
Notice it is fairly easy to read and understand which makes it easy to edit as well. Now that you have a working macro you should visit vstipTool0066 (https://blogs.msdn.com/b/zainnab/archive/2010/08/07/create-a-shortcut-key-for-a-macro-vstiptool0066.aspx) and create a shortcut key for it.
Comments
Anonymous
December 01, 2010
Another cool thing you can do with macros is automatic flicking between related source files (e.g. .cpp and .h in C++), like some add-ins give you but without having to install an add-in (and with being able to customize the rules for your projects). A macro I threw together to do that is in the answers here, and I just noticed a comment linking to an alternative macro which is a bit better than mine: stackoverflow.com/.../in-visual-studio-2010-how-do-i-quickly-switch-from-a-h-file-to-the-correspondin My other favourite use for macros is providing a toolbar button I can click to attach the debugger to the running process I often want to debug, without requiring any interaction beyond clicking the toolbar button. VS macros are great! I think the challenge is remembering they exist. :) Until that question came up on SO I'd been missing the header-flick feature from the old WndTabs VS6 add-in but kept forgetting that I could easily make such a thing myself using a macro. The question came up and a few minutes later I had what I had wanted for ages (and what the question-asker wanted, too). Now that I think of it, I should make macros to open the projects I commonly use so I don't get frustrated when they go off the Recent list. (I'm still using VS2008 so don't have VS2010's cool ability to "pin" certain projects.)Anonymous
December 01, 2010
That's awesome stuff Leo! I hope folks keep the C++ stuff coming!Anonymous
October 04, 2012
how can i create a MACRO in MS PROJECT 2010 that will automatically change the color of my gannt bar based on my start and finish dates? can you please help me on this?Anonymous
October 04, 2012
i will appreciate your help. you can e-mail me at frenchwitch_21@yahoo.com.sg thanks