Custom Wrap Macro for Visual Studio

This content may be moving. Please update your bookmark to http://vdir.us/go/vs-wrap-macro.

As an update to my last post, Using VS Macros to Speed Up Text Entry, here is a block of code that allows you to enter any string as the HTML/XML node name that wraps the current selection. Note that you’ll need to setup the Constants class to hold the shared variable.

Public Class Constants
    Public Shared CustomDefaultValue As String = "p"
End Class

Public Module BasicMacros
    Public Sub WrapInCustom()
        ' Get the input
        Dim str As String = InputBox("Wrap the selection in what?", "Wrap in Custom", Constants.CustomDefaultValue)

        ' Empty string == cancel!
        If (String.IsNullOrWhiteSpace(str)) Then Return

        ' Store the value so that it is easy to repeat
        Constants.CustomDefaultValue = str

        ' Get the current selection
        Dim ts As TextSelection = CType(DTE.ActiveDocument.Selection, TextSelection)

        ' Add the value
        ts.Text = "<" + str + ">" + ts.Text + ""
    End Sub
End Module