Region Macro For Visual Studio

I created this before the code snippit region came along. This macro will work with VS2003 and VS2005. Simply highlight the code you want and a dialog box will come up asking for the name. Note depending on the location the #region and #endregion will be at those positions.

Sub RegionIt()
    Dim sWhat As String
    sWhat = InputBox("Region Name")
    If (sWhat <> "") Then

      Dim selection As TextSelection = DTE.ActiveDocument.Selection()
      Dim start As EditPoint = selection.TopPoint.CreateEditPoint()
      Dim endpt As EditPoint = selection.BottomPoint.CreateEditPoint()

      DTE.UndoContext.Open("Comment Region")
          start.Insert("#region " & sWhat & Chr(13))
          endpt.Insert(Chr(13) & "#endregion")
          'If an error occured, then need to make sure that the undo 
          'context is cleaned up. Otherwise, the editor can be left 
          'in a perpetual undo context
      End Try

    End If
End Sub

Leave a Reply