Comment / Uncomment macros

These macros were contributed by Guy Gascoigne - Piggford

Here are a couple of macros that I find very useful, they allow you to comment or uncomment out the current line or selection. CommentOut is based upon a similar macro that was shipped with Visual Studio but trimmed to suit my needs better, the UnCommentOut function will reverse it's effects.

To use: take these two definitions and paste them into your current macro file, and save. For ease of use I bind these to a pair of keys, in my case I bind Ctrl+/ to CommentOut and Ctrl+. to UnCommentOut.


Sub CommentOut ()
	'DESCRIPTION: Comments out a selected block of text.
	Dim win
	set win = ActiveWindow
	if win.type <> "Text" Then
	  MsgBox "This macro can only be run when a text editor window is active."
	else
		CommentType = "//"

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

		If EndLine = StartLine Then
			ActiveDocument.Selection = CommentType + ActiveDocument.Selection
		Else 
			For i = StartLine To EndLine
				ActiveDocument.Selection.GoToLine i
				ActiveDocument.Selection.SelectLine
				ActiveDocument.Selection = CommentType + _
				ActiveDocument.Selection
			Next
		End If
	End If
End Sub


Sub UnCommentOut ()
'DESCRIPTION: Uncomments a selected block of text.
	Dim win
	set win = ActiveWindow
	if win.type <> "Text" Then
	  MsgBox "This macro can only be run when a text editor window is active."
	else
		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
			TmpBlock = ""

			ActiveDocument.Selection.GoToLine i
			ActiveDocument.Selection.SelectLine
			CmtBlock = ActiveDocument.Selection

			Trim(CmtBlock)

			If Instr(CmtBlock,"//") <> 0 Then 
				TmpBlock = TmpBlock + Left (CmtBlock, Instr (CmtBlock,"//") - 1)
				CmtBlock = Right(CmtBlock, (Len(CmtBlock) - (Instr(CmtBlock, "//") + 1)))
				CmtBlock = TmpBlock + CmtBlock
			End If

			ActiveDocument.Selection = CmtBlock
		Next

	End If
End Sub

Here's an update by Todd Crooks.

It uses regular expressions to add or remove comments.


Sub BlockComment()
     ActiveDocument.Selection.ReplaceText "^", "//", dsMatchRegExp
End Sub
 
Sub BlockUncomment()
     ActiveDocument.Selection.ReplaceText "^//", "", dsMatchRegExp
End Sub

Updated on: October 30, 1998



Comments

  • better handling of indented code

    Posted by rogerdahl on 05/16/2005 02:41pm

    This takes whitespace in front of comments into account
    
    Sub BlockComment()
         ActiveDocument.Selection.ReplaceText "^\(\:b+\)", "\1//", dsMatchRegExp
    End Sub
    
    Sub BlockUncomment()
         ActiveDocument.Selection.ReplaceText "^\(\:b*\)//", "\1", dsMatchRegExp
    End Sub

    Reply
  • Change so that one function comments/uncomments

    Posted by Legacy on 03/06/2000 12:00am

    Originally posted by: Richard Samuels

    I changed this so that I wouldn't have to assign two keys to do the commenting and uncommenting. If the selected text 
    
    begins with a commented line, the selection is uncommented, and if it doesn't, the selection is commented!


    Sub CommentBlock ()
    'DESCRIPTION: Comments or uncomments a selected block of text.
    Dim win
    set win = ActiveWindow
    if win.type <> "Text" Then
    MsgBox "This macro can only be run when a text editor window is active."
    else
    If Left( ActiveDocument.Selection, 2 ) = "//" or Left( ActiveDocument.Selection, 2 ) = "/*" Then
    UncommentBlock()
    Else
    If InStr( ActiveDocument.Selection, vbCr ) > 0 then
    ActiveDocument.Selection.ReplaceText "^", "//", dsMatchRegExp
    Else
    ActiveDocument.Selection = "/* " + ActiveDocument.Selection + " */"
    End If
    End If
    End If
    End Sub

    Sub UnCommentBlock ()
    'DESCRIPTION: Uncomments a selected block of text.
    Dim win
    set win = ActiveWindow
    if win.type <> "Text" Then
    MsgBox "This macro can only be run when a text editor window is active."
    else
    ActiveDocument.Selection.ReplaceText "\/\* ", "", dsMatchRegExp
    ActiveDocument.Selection.ReplaceText "\*\/", "", dsMatchRegExp
    ActiveDocument.Selection.ReplaceText "^//", "", dsMatchRegExp
    End If
    End Sub
    </PRE>

    Reply
  • Minor change for toggling comments

    Posted by Legacy on 12/14/1999 12:00am

    Originally posted by: Nils Kronqvist

    Just combined the macros to toggle commenting instead.
    
    I'm sure it can be made more robust etc. but works fine
    for me. I used to miss the comment/uncomment feature in
    VB6 in the VC6 env - good thing I stumbled on this.
    Thanks!

    Sub ToggleComment()
    'DESCRIPTION: Toggles commenting of selected text.

    Dim win
    set win = ActiveWindow
    if win.type <> "Text" Then
    MsgBox "This macro can only be run when a text editor window is active."
    exit sub
    end if

    firstChars = Left(ActiveDocument.Selection, 2)
    if (firstChars = "//" Or firstChars = "/*") then
    'Remove comments
    ActiveDocument.Selection.ReplaceText "\/\* ", "", dsMatchRegExp
    ActiveDocument.Selection.ReplaceText "\*\/", "", dsMatchRegExp
    ActiveDocument.Selection.ReplaceText "^//", "", dsMatchRegExp
    Else
    'Add comments
    if InStr( ActiveDocument.Selection, vbCr ) > 0 then
    ActiveDocument.Selection.ReplaceText "^", "//", dsMatchRegExp
    Else
    ActiveDocument.Selection = "/* " + ActiveDocument.Selection + "*/"
    End If
    End If
    End Sub

    Reply
  • More about comment/uncomment macros

    Posted by Legacy on 11/03/1998 12:00am

    Originally posted by: Guy Gascoigne - Piggford

    That's very neat and much simpler (and quicker) than the way that I was doing it.
    
    

    Here is a slight variation that copes with commenting out part of a line, say when you want to comment out an unused parameter quickly.

    Sub CommentOut ()
    'DESCRIPTION: Comments out a selected block of text.
    Dim win
    set win = ActiveWindow
    if win.type <> "Text" Then
    MsgBox "This macro can only be run when a text editor window is active."
    else
    if InStr( ActiveDocument.Selection, vbCr ) > 0 then
    ActiveDocument.Selection.ReplaceText "^", "//", dsMatchRegExp
    Else
    ActiveDocument.Selection = "/* " + ActiveDocument.Selection + " */"
    End If
    End If
    End Sub

    Sub UnCommentOut ()
    'DESCRIPTION: Uncomments a selected block of text.
    Dim win
    set win = ActiveWindow
    if win.type <> "Text" Then
    MsgBox "This macro can only be run when a text editor window is active."
    else
    ActiveDocument.Selection.ReplaceText "^//", "", dsMatchRegExp
    End If
    End Sub

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds