Macros in Visual Studio 2005

By Akbar

Most of the developers follow some standards for the headers and change signatures in the software projects for change management and tracking and I’m no exception (and wouldn’t want to be in this case). I have used Macros in different tools to make this logging process standardized.

But for some reason I never thought of using Macros in Visual Studio 2005. When working in the VS projects, though we defined a standard for the Headers and Change Signatures, but as you might have guessed, as the developers started working (me including), small variation started appearing in these formats and eventually we have set of different styles of comments and signatures. Yesterday one of my co-worker suggested to use the Macros to standardized this and I said Yes, that should be the way to do it! I then wondered why I haven’t thought of this before. May be I was too busy with other important stuff 😉

Anyway, long story short, I explored the Macro in Visual Studio 2005 and was really really amazed. Though Macros writing and recording style is very similar to most of the other tools by Microsoft, but what really impressed me was how easily you can call these form you IDE. My favorite one was adding this to Contextual Menu. So I can now run these macros without even leaving the current code window. It really helps, trust me on this.

Here is a very short jump start to get you started. You can read more on the MSDN.

1) Launch Macros IDE ( Alt + F11)

2) In the Macros IDE, you should see a default Macro project “MyMacros”. You can use that or if you really need to create a new one, you can create it from the Tool | Macros | New Macro Project menu option.

3) Add a new Module to the MyMacros project by Right clicking on the project and then choosing Add | Add Module.

4) Now create a Macro to insert the dynamic signature at the current cursor position. So here is what I added in the code:

Public Sub InsertSignature()
Dim editPoint As EditPoint = DTE.ActiveDocument.Selection.ActivePoint.CreateEditPoint
“// “ + strDevInitials + “-“ + DateTime.Now.ToShortDateString() + “-“ + strVersion + ” – “)
End Sub

Some of you may be disappointed to see the VB code and may search for the C# code to do. I tried the same, but I don’t see a way to do this in C#. But hey, VB is not that bad, if you don’t know it already, this is a good point to learn this simple language.

5) Ok, now with this you have your Macro ready. Pretty simple, isn’t it. Now the big deal, how to call this from the VS IDE. If I have to click several keys to launch this Macros, then there is really no sense of using the Macros, right? So we need a quick and fast way to do it. Fortunately VS provide it and my favorite one is adding  this in the Contextual menu. Though you can add it to also to any Visual Studio Menu, Toolbars too. Here is how to add this to a Context Menu:

5.1 ) From the Visual Studio IDE, Choose Tools | Customize 5.2 ) In the Customize window, check the “Context Menus” from the Toolbars area. If you done it correctly, you should see a new toolbar at top which have most of the Contextual Menus you see while working in the VS IDE. Locate the correct one (I choose Editor Context Menu | Code Window).5.3) Now explore that Context Menu, and while it’s explored/open, go to the Commands tab of the Customize Window and click the Macros.5.4) Now find and drag you Macro (InsertSignature) and drop it over the desired point in the Context Menu.

5.5) You can now rename this new menu text, add picture, make it a group, and what not. Just explore it to be amazed.

5.6) Visual Studio 2005 also have a nice Command option called “New Menu”. As name suggest it let’s you create custom sub menus. So say Good bye to menu clutter.

5.7) Now save your solution and Visual Studio will auto load this new Macro and Context Menu item in the future too.

That’s all for now. I hope this will help you all too.


Tags: ,