Archive for the ‘Macros’ Category.

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

Visual Studio Macro – Property Generation

Before the intellisense operation prop came about, I had a need to do something similar but to add comments above the property and variable. The below macro, which works in 2005 and 2003 versions of Visual Studio allows one to create a property with comments. The trick is to create a mask/template and fill it with the specifics of what was queried from the user.

Here is the template and supporting variables

Private Cr As String = Environment.NewLine
Private Start As String = "{"
Private Close As String = "}"

Private mask As String = _
	"/// <summary>{3}</summary>" + Cr + _
	"private {0} {2};" + Cr + _
	"/// <summary>{3}</summary>" + Cr + _
	"public {0} {1}" + Cr + "{{" + Cr + _
	"get {{ return {2};  }}" + Cr + _
	"set {{ {2} = value; }}" + Cr + "}}"

Here is the function which should be excecuted to begin the process. Note it simply queries the user for the information, loads the mask and sends it to be outputted.

Public Sub GeneratePublicProperty()
	Dim PropertyType As String = _
	  InputBox("Property Return type:", "Property Type")

	Dim PropertyName As String = _
	  InputBox("Property Name:", "Property Name")

	Dim FieldName As String = _
	  InputBox("Internal Variable name:", "Field Name")

	Dim Summary As String = InputBox("Summary:", "Summary")

	Dim Code As String = String.Format(mask, PropertyType, PropertyName, FieldName, Summary)


End Sub

Here is the code that outputs it to the screen.

Private Sub PasteAlign(ByVal Code As String)

	' Debug.WriteLine(Code)

	Dim ts As TextSelection = DTE.ActiveWindow.Selection

	ts.Insert(Code.ToString(), vsInsertFlags.vsInsertFlagsInsertAtStart)

End Sub

Alignment Macro for Visual Studio 2003/2005


In all of my editors I have used, I have always written a macro to align equal signs in code. For me code has to be aesthetically pleasing and this process helps that by making the equal signs as well as other items line up.

The macro purpose is to align target text on the current and following lines looking for the text until it can no longer find it. At that point it stops, calculates which point is the farthest out and then moves all the other lines to that location.

There are actually two macros, one to only align just = signs and the other which asks for target text to align.

Equal Before
Any Before
Any Ask
There are actually two macros in this package for extra flexibility. Besides just aligning equal signs…the other macro will align anything. To do that it will prompt the user as to what to align. That can be either one character or a word or phrase.
Any Ask
Equal After Any After

  • Macro starts at current text editing position.
  • Tabs in document may interfer with calculation. If that is the case remove tabs or make sure they are evenly placed for all lines in question.
  • There are two public macros in this package (Align and AlignIt). Its best to assign them to similar keystrokes like Alt-F10 and Ctrl-Shift-F10.
  • This is a non-destructive macro, it will not delete lines or move items to other lines.
  • Once it cannot find the target text it will stop.
  • Download is in the form of a zip file which contains the vb code and a readme. There should not be viruses…but it is always good to check.


Align VB Macro for VS 2003/05 in Zip