新建宏文件,贴代码:
'多行注释
Sub Comment()
if Documents.Count = 0 then
exit sub
end if
lTopLine = ActiveDocument.Selection.TopLine
lBottomLine = ActiveDocument.Selection.BottomLine
lInsertPoint = ActiveDocument.Selection.CurrentLine
For I = lTopLine To lBottomLine
ActiveDocument.Selection.MoveTo I, 1
ActiveDocument.Selection.SelectLine
s = ActiveDocument.Selection.Text
if s <> vbCrLf then
s = "//" + vbTab + s
end if
ActiveDocument.Selection.Text = s
Next
if lTopLine = lInsertPoint then
ActiveDocument.Selection.MoveTo lBottomLine, dsEndOfLine
ActiveDocument.Selection.MoveTo lTopLine, 1, dsExtend
else
ActiveDocument.Selection.MoveTo lTopLine, 1
ActiveDocument.Selection.MoveTo lBottomLine, dsEndOfLine, dsExtend
end if
End Sub
'多行反注释
Sub Uncomment()
if Documents.Count = 0 then
exit sub
end if
lTopLine = ActiveDocument.Selection.TopLine
lBottomLine =ActiveDocument.Selection.BottomLine
lInsertPoint = ActiveDocument.Selection.CurrentLine
For I = lTopLine To lBottomLine
ActiveDocument.Selection.MoveTo I, 1
ActiveDocument.Selection.SelectLine
s = ActiveDocument.Selection.Text
while left(s, 1) = " " OR left(s, 1) = vbTab
s = right(s, len(s) - 1)
Wend
if left(s, 3) = "//" + vbTab then
s = right(s, len(s) - 3)
elseif left(s, 2) = "//" then
s = right(s, len(s) - 2)
end if
ActiveDocument.Selection.Text = s
Next
if lTopLine = lInsertPoint then
ActiveDocument.Selection.MoveTo lBottomLine, dsEndOfLine
ActiveDocument.Selection.MoveTo lTopLine, 1, dsExtend
else
ActiveDocument.Selection.MoveTo lTopLine, 1
ActiveDocument.Selection.MoveTo lBottomLine, dsEndOfLine, dsExtend
end if
ActiveDocument.Selection.SmartFormat
End Sub
关闭并保存。
工具-->定制-->附加项和宏文件-->浏览-->选择刚才创建的宏文件-->打上√-->命令-->在类别中选择macros-->在右侧命令中拖出刚才生成的两个宏名(comment和Uncomment)到工具条上-->分别选择图标。
设置快捷键:键盘-->类别中选择Macros,在命令中选择comment或Uncomment然后设置快捷键即可。