Block Comment Macro for Visual Studio .NET

Environment:

Here’s a block comment macro for Visual Studio .NET (as I wasn’t able to find one and I wanted to find the differences in the API). It seems to work okay for me. Feel free to post bug fixes and so forth. I hope someone finds it useful. Good luck.

The code follows:

' This will comment/uncomment out blocks. Blocks are
' commented at the beginning of the line.
' Assign this to a key (e.g. ctrl-/) and it will toggle the
' current line/block of code.
' This will handle both "//" and "'" style comments. Adapted
' to VS.NET by Tim Stubbs
' Original code from the VS example and (idea from) Adam Solesby
' who originally did this for VC 6 (or earlier). There's been
' quite a few changes to VS.NET so this took a little creative
' adaptation. I hate VB :) Disclaimer: Use at own risk. I did
' it for ME!
' I don't need anything other than block commenting tbh, so
' I've not special cased single line selection.
Sub CustomCommentOutVSNET()
  'DESCRIPTION: Comments out a selected block of text.
  Dim win As Window
  win = ActiveWindow()
  If win.Type <> EnvDTE.vsWindowType.vsWindowTypeDocument Then
    MsgBox("This macro can only be run when a text editor _
            window is active.")
  Else
    TypeOfFile = FileType(ActiveDocument)
    ' MsgBox "Type: " + CStr(TypeOfFile)
    If TypeOfFile > 0 And TypeOfFile < 6 Then
      If TypeOfFile > 3 Then
        CommentType = "'"    ' VB bah. humbug.
        CommentWidth = 1
      Else
        CommentType = "//"   ' C++ and java style comments
        CommentWidth = 2
      End If

      StartLine = ActiveDocument.Selection.TopLine
      EndLine = ActiveDocument.Selection.BottomLine
      If EndLine < StartLine Then
        Temp = StartLine
        StartLine = EndLine
        EndLine = Temp
      End If


      For i = StartLine To EndLine

        ActiveDocument.Selection.GoToLine(i)

        ' check for nasties like blank lines or short lines
        ActiveDocument.Selection.EndOfLine(True)
        Dim objActive As VirtualPoint = _
            ActiveDocument.Selection.ActivePoint
        Dim right, line As Integer
        right = objActive.DisplayColumn

        ActiveDocument.Selection.StartOfLine _
          (vsStartOfLineOptionsFirstColumn)
        ActiveDocument.Selection.CharRight _
          (True, CommentWidth)

        line = objActive.Line()
        If (line > i) Then     ' we've shifted line, _
            reset to original line
            ActiveDocument.Selection.GotoLine(i)
            ActiveDocument.Selection.EndOfLine(True)
        End If

        'Enable the following for debugging
        'Dim blah, wibble As String
        'wibble = ActiveDocument.Selection.text()
        'blah = CommentType

         If ActiveDocument.Selection.text() = CommentType _
         Then
            ActiveDocument.Selection.Delete()
         Else
            ActiveDocument.Selection.EndOfLine(True)
            wibble = CommentType + ActiveDocument() _
                     .Selection.text()
            ActiveDocument.Selection.text() = CommentType _
            + ActiveDocument().Selection.text()
         End If

      Next
    Else
      MsgBox("Unable to comment out the highlighted text" _
              + vbLf + _
       "because the file type was unrecognized." + vbLf + _
       "If the file has not yet been saved, " + vbLf + _
       "please save it and try again.")
    End If
  End If
End Sub

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read