Increase Productivity with Visual Studio Macros

Provided by the FMS Development Team

Visual Studio Macros are one way to increase productivity when a particular action or task needs to be repeated multiple times. Addins are another. Almost anything that can be done in the Visual Studio Integrated Development Environment (IDE) manually can be done through macros, except for thinking!

A good place to start with macros is playing with the interesting samples shipped with Visual Studio. You can view the samples using the Macro Explorer.

  1. Fire up Visual Studio.
  2. Navigate to menu View->Other Windows->Macro Explorer or press Alt+F8.
  3. Click on Samples (which is a macros project).

Macros under VSEditor are especially interesting. Open a project in the editor and run the macros by right clicking and choosing Run.

Right clicking on the macro and choosing Edit brings up the Visual Studio Macro editor. You can either create new modules, macros or edit existing ones. The Macro editor has the same look and feel as the Visual Studio editor, so getting around isn’t difficult. Every macro that you write is basically a VB procedure.

Debugging Visual Studio macros are a breeze! Just place a break point in the Macro editor, jump back to the Visual studio editor and run the macro.

The Visual Studio editor itself is efficient at productivity. For e.g. the Comment and Uncomment selection feature is a boon for getting code out of the way which it does, by individually commenting the selected lines. However for using C style multi-line comments like /* .. */ there is no way to do this.

For example I want to comment the first parameter bar in Foo() below:

private void Foo(int bar, string name)
{
      ...
}

There is no built in way to do so; Visual Studio macros to the rescue! Using the Visual Studio automation model it can be done easily. The following function comments out selected code using multi line comments. Copy this function to the Visual Studio Macro editor. To use this macro, simply select code anywhere in the Visual Studio Editor and run it.

Here is the Sample Code

  1. Fire up Visual Studio.
  2. Navigate to menu View | Other Windows | Macro Explorer or press Alt+F8.
  3. Double-click on MyMacros.
  4. Click on Module1 (which is the default or create a new module).
  5. Right-click on Module1 and choose Edit. (The VS Macros IDE should show).
  6. Copy the above functions within Module1.
  7. Click File | Save MyMacros.
  8. Return to Visual Studio IDE, and open a project.
  9. Keeping the cursor on a code window, Double-click on the FMS_CommentCStyle or FMS_UnCommentCStyle() macros.

The logic itself is pretty straight forward. As soon as we are invoked, get the selection, its location, process it and re-insert if required, back in to the source.

The “DTE”(Development Tools Extensibility) object represents the instance of the Visual Studio development environment in which this macro is hosted. This is available as a static or shared member of the EnvDTE library for building macros. One of the many members of the DTE object is the ActiveDocument property. This represents the current document (windows form, web form, code window) open in the Visual Studio IDE and exposes it through the EnvDTE.Document interface. This is the place we want to start at, since most of our work would be on a text document. The ActiveDocument.Selection property returns an Object, which we contain in a TextSelection interface. This represents the current text selection in the active document open in the Visual Studio IDE. This would however fail if the document open is a designer document like a form or UserControl.

Once we have the text selection, we operate on it. We try and make life a little easy on the user if there is no selection, by working on the current line, similar to the single line commenter of Visual Studio. In other words, if there is no selection we assume that the user wants to comment the entire single line and simulate the selection. Next, delete the selection and insert new processed text.

Similar logic applies to the FMS_UnComment() function. The only difference, is the use of string.Replace() to delete comment characters.

To support undo logic in one step, we use the DTE.UndoContext(). Once either of these macros has been successfully used, the Edit->Undo and Edit->Redo features can be used reliably.

You can add more frills to this simple functionality and increase its usability by adding the date (System.DateTime.Today) and the user name (System.Environment.UserName) or any other standards that your company requires, within the comments.


Additional Resources

 

 

Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.

Darren D.


View all FMS products for Microsoft Access All Our Microsoft Access Products

 

 

Free Product Catalog from FMS