Shaykatc's WebLog

VS 2003 Tip #2: Macros

Vs 2003 allows you to write macros that can automate the IDE. You can acess these through the Macro Explorer - just go to View->Other windows->Macro Explorer and look through the Macros already present there. You'll see we provide a set of sample macros that do various cool things such as automating the editor and the debugger. Just right click on one, choose Edit and view the code. You'll see these work by calling through the automation model in VS. These macros can be called from the command window or by double clicking and running the macro from the Macro Explorer. In Everett though these could only be written in VB.

One of the macros we wrote was for a debugger specific issue a while ago. Our QA would find a hang when debugging a program and would write up a bug report for it. We would always ask for call stacks - however it was painful to look through each thread and grab each and everyones call stack and paste it. So we wrote the DumpStacks macro which generated the callstacks for each thread. Simple.

  • Do you have any cool macros out there that you use (I'm trying to dig up an old favorite of ours that dumped out the keyboard commands right now)?
  • On a seperate note, have many of you used this functionality before or knew it existed? We had a discussion about this today- about whether C# customers knew about this functionality at all, so any feedback would be appreciated!

Addendum: A co-worker and I were further discussing this and thought it would be cool to hear feedback if anyone relied on this extensively too? Let us know!

Published Wednesday, February 18, 2004 11:56 AM by shaykatc

Comments

 

Me said:

I tried once to create a macro to toggle the "Enable HTML Validation" setting in in Tools-Options-Text Editor-HTML/XML-HTML Specific. The macro recorder only recorded

DTE.ExecuteCommand("Tools.Options")

which was useless. Couldn't find any useable property to achieve this so I gave up with the impression that the IDE automation in VS.NET leaves a lot to be desired.
February 18, 2004 1:03 PM
 

Wes said:

I'm a C# developer and I know it exists and I have written various little macros. I would like to see the ability to write macros in C#. Not that I'm a VB hater but I just prefer C#.
February 18, 2004 1:43 PM
 

Louis Parks said:

I knew it was there...I've seen the vsmacros folder. I've never used them though.
February 18, 2004 2:05 PM
 

milbertus said:

I knew it was there, too, and was initially surprised that macros could only be written using VB (given the whole "every language is equal" .NET philosophy). However, assuming that VS uses VSA for its automation and macros, I recall reading somewhere that C# can't be used for VSA objects, due to some late binding issue. Which sucks, but I'll deal.
February 18, 2004 2:39 PM
 

Blair Stephenson said:

I use a macro developed by someone else, that copied the compiled assembly to a specific location. (1.0 days)

We use file references as have found less issues then using project references. This helps with that process.
February 18, 2004 3:11 PM
 

Tony Chow said:

I use macros to:

1) Insert bracket pairs and return cursor to the line between the brackets. Now my code is full of one-liner brackets!

2) Insert GUIDs into code.

3) Mark critical comments with name and date, such as "[Tony Chow 5/4/2003]"

4) Perform massive search and replace operations that require more than one pass of regular expression queries.

So there.
February 18, 2004 3:40 PM
 

James Geurts said:

I wrote and use a macro that auto increments the build number in an AssemblyInfo.cs file. It's located here: http://blogs.biasecurities.com/jim/archive/2003/10/08/166.aspx
February 18, 2004 4:19 PM
 

Roland Weigelt said:

I often use temporary macros. And I couldn't live without some macros I wrote for expanding/collapsing #regions in C# code (located here: http://weblogs.asp.net/rweigelt/archive/2003/07/06/9741.aspx).
February 19, 2004 1:40 AM
 

Dan Szepesi said:

There is a difference between knowing about them, and kind of realizing that they can be used for pretty neat purposes.

I knew they were there, but never really took the time to think about how I could exploit their use. Examples are a great way to kick start that thought process - thanks for bringing this up.
February 19, 2004 8:01 AM
 

Frank Hileman said:

I have a whole boatload of macros I use all the time for writing C# code. Originally I wrote them for C++ in VS6 and ported when VS.NET came out. I use them with a toolbar and pen, but anyone who wants to use them with keyboard accelerators can do that. These macros save me a lot of time by writing all control structures, commenting, doc comments, modifiers, property get and sets, everything.

I uploaded the file to:
http://www.vgdotnet.com/Editing.vb

To use them from the toolbar, run the aaAddToolbarAndButtons. Then right click on your empty toolbar area and check the "Editing Macros" toolbar to make it visible. You will see some cryptic menu titles (3 letters each for space), but if you click on each menu, you will see that the items within each menu are completely self-explanatory. Many will operate on the currently selected text, if you have any. All code insertion macros insert new code below the current line.

These macros are also great for people who control Visual Studio with voice recognition. Just associate them with keyboard shortcuts, and make your VR software spit out those shortcuts.

Thanks so much for making macros so powerful in VS! They make a huge difference for me!

Here is a picture of the cryptic toolbar, on the right-hand side:
http://www.vgdotnet.com/screen1.png
February 24, 2004 11:13 AM
 

William Robertson said:

I found the macros recently and use them often

Placing Region blocks around methods and properties

Moving my web.config file over from release mode settings to debug mode settings (I found the preprocessor directives don't work well in there *smirk*)

Grabbing my select list from a stored procedure and populating text boxes. (naming is consistent)

and many more...
February 25, 2004 3:00 PM
 

Steve Hiner said:

I've been waiting for macros in VB for years so now that I have them I've written a few. Mostly little utility fuctions that do things like wraping a selection in a Region or a Try..Catch, changing a private or public variable into a property.

My personal favorite is one that flips the selecting around an assignment. So I can take something like:
Textbox1.Text = m_Varible1
Textbox2.Text = m_Varible2

and swap it around to:
m_Varible1 = Textbox1.Text
m_Varible2 = Textbox2.Text

I used to have to do that a lot in the VB5/6 days when making custom controls - all the property bucket junk. At the time I would have loved to have a macro that would handle it. It's also quite useful when doing DB work when you often pull values out of a row and later need to put the values back in.

I really wish there was a site that would make it easy for all of us to share our macros. I think MS would be wise to create such a site, even if it was just a new section at gotdotnet.com.
March 2, 2004 12:11 PM
 

John S. said:

Use them. Love it Thanks!

The most popular here are:
- Automating Word to generate a push report of my checked-out files (in our preferred style) before starting my pushes.
- Copying build output to differnt folders.
- Minor source tweaks as others have mentioned.
March 3, 2004 10:32 AM
 

Bill Menees said:

I've written a lot of macros for work and home. I have macros to:
Wrap the selected block in a region.
Add a formatted TODO comment.
Build just the startup project and its dependencies.
Add a formatted document header/revision history block.
Configure a C# project's setting to match our corporate standards.
Do many other repetitive things that I have to do.

Some of these I've ported up from VC6's VBA macros. Some I've written since VS2002 came out. Almost every month I find something new I can do with VS macros.

FWIW, I also use the TemporaryMacro feature constantly, and I occasionally save off its output as a starting point for a permanent macro.

My only two complaints are that I still have to write in VB.NET and that there's no support for a form designer. The reason for the first point should be obvious to someone on the C# team. The reason for the second point is that some of my macros need to popup dialogs, and I have to build them by hand. Actually, I build the dialogs in a new VB.NET Windows Application, and then I copy the code into my macro project and use the form class from there.

But overall, the macro support in VS rocks and is absolutely critical to how I do my work and how my team members work.

March 12, 2004 6:32 PM
 

Code/Tea/Etc... said:

March 14, 2004 5:00 AM
 

Code/Tea/Etc... said:

March 14, 2004 5:03 AM
 

Jon Galloway said:

This got me fired up to start using my macros again. When I did, I found that they've stopped working - when I try to run them, the little macro notification tray icon flickers and disappears, but the macro doesn't do anything (no affect on code window). No errors, messageboxes, etc.

When I try to debug the macros, code execution doesn't stop at breakpoints.

Anyone else seen this? Any suggestions?
March 14, 2004 12:12 PM
 

Bill Menees said:

I saw that when I installed VS2002 after VS2003. VS2002 broke the macro support for VS2003. I had to reinstall VS2003 to get things reregistered correctly.
March 14, 2004 7:58 PM
 

Sonia Kaushal said:

hi read this and do u r project about micro
March 15, 2004 7:12 AM
 

Tony Stewart said:

I new to macros writing and just want to set up one to insert signatures in new emails, can and how is this done?
March 15, 2004 11:41 AM
 

Code/Tea/Etc. said:

August 25, 2004 6:17 AM
 

Shaykatc s WebLog VS 2003 Tip 2 Macros | debt consolidator said:

June 19, 2009 10:50 AM
New Comments to this post are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker