pondelok, 21. septembra 2009

Visual Studio Macros

I think Visual Studio macros is interesting feature which is not often used. Using macros you can automate repetitive tasks and increase your development productivity. You can create your own macros using Macro Exlorer (View - Other Windows - Macro Explorer).

My sample shows macro for generating new GUID in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} format, which is common task at SharePoint development when writing some stuff in CAML:

   1:  Imports System

   2:  Imports EnvDTE

   3:  Imports EnvDTE80

   4:  Imports EnvDTE90


   6:  Public Module SharePointMacros

   7:      Sub CreateNewGuid()

   8:          Dim doc As Document = DTE.ActiveDocument

   9:          Dim textDoc As TextDocument = CType(doc.Object("TextDocument"), TextDocument)


  11:          textDoc.StartPoint.CreateEditPoint()

  12:          textDoc.Selection.Insert("{" + Guid.NewGuid.ToString() + "}")

  13:      End Sub

  14:  End Module

To associate your macro with keyboard shortcut go to Tools - Options - Environment - Keyboard, find macro in command list, press shortcut (CTRL+N, CTRL+G) and assign it.

Setting VS keyboard shortcut

When you press the shortcut in code editor, macro will be run and new GUID will be inserted on the cursor position.

APress - Writting Add-Ins for Visual Stutio .NETWiley - Developing Visual Studio .NET Macros and Add-Ins