What are some macros that you have found useful in Visual Studio for code manipulation and automation?


You might want to add in code snippets as well, they help to speed up the development time and increase productivity.

The standard VB code snippets come with the default installation. The C# code snippets must be downloaded and added seperately. (Link below for those)

As far as macros go, I generally have not used any but the working with Visual studio 2005 book has some pretty good ones in there.

C# Code snippets Link: (Jeff Atwood provided the link) HIH

@RZachary - I think that code snippets are independent enough of macros that I went ahead and created a new question for them. It can be found here.
+5  A: 

This is one of the handy ones I use on HTML and XML files:

Option Strict Off
Option Explicit Off
Imports EnvDTE
Imports System.Diagnostics

Public Module ReplaceUnicodeChars

    Sub ReplaceUnicodeChars()
        ReplaceAllChar(ChrW(8230), "…")   ' ellipses
        ReplaceAllChar(ChrW(8220), "“")   ' left double quote
        ReplaceAllChar(ChrW(8221), "”")   ' right double quote
        ReplaceAllChar(ChrW(8216), "‘")   ' left single quote
        ReplaceAllChar(ChrW(8217), "’")   ' right single quote
        ReplaceAllChar(ChrW(8211), "–")   ' en dash
        ReplaceAllChar(ChrW(8212), "—")   ' em dash
        ReplaceAllChar(ChrW(176), "°") ' °
        ReplaceAllChar(ChrW(188), "¼") ' ¼
        ReplaceAllChar(ChrW(189), "½") ' ½
        ReplaceAllChar(ChrW(169), "©") ' ©
        ReplaceAllChar(ChrW(174), "®") ' ®
        ReplaceAllChar(ChrW(8224), "†")   ' dagger
        ReplaceAllChar(ChrW(8225), "‡")   ' double-dagger
        ReplaceAllChar(ChrW(185), "¹") ' ¹
        ReplaceAllChar(ChrW(178), "²") ' ²
        ReplaceAllChar(ChrW(179), "³") ' ³
        ReplaceAllChar(ChrW(153), "™")   ' ™
        ''ReplaceAllChar(ChrW(0), "�")

    End Sub

    Sub ReplaceAllChar(ByVal findWhat, ByVal replaceWith)
        DTE.Find.FindWhat = findWhat
        DTE.Find.ReplaceWith = replaceWith
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
    End Sub

End Module

It's useful when you have to do any kind of data entry and want to escape everything at once.

+1  A: 

This is one I created which allows you to easily change the Target Framework Version of all projects in a solution:

Scott Dorman
+1  A: 

I'm using Jean-Paul Boodhoo's BDD macro. It replaces whitespace characters with underscores within the header line of a method signature. This way I can type the names of a test case, for example, as a normal sentence, hit a keyboard shortcut and I have valid method signature.

+4  A: 

This is my macro to close the solution, delete the intellisense file, and reopen the solution. Essential if you're working in native C++.

Sub UpdateIntellisense()
    Dim solution As Solution = DTE.Solution
    Dim filename As String = solution.FullName
    Dim ncbFile As System.Text.StringBuilder = New System.Text.StringBuilder
    ncbFile.Append(System.IO.Path.GetDirectoryName(filename) + "\")
End Sub
John Richardson
ncbString is never used
@xan: Removed the ncbString for JR
Jon Cage