Comment / Uncomment macros | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 30, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.