利用VBA 宏实现vc6.0的自动添加注释和自动取消注释

 

虽然一下方法可以实现,但是安装 Visual_Assist_X_10.7.1925.0 后可以自动实现这些功能,而且感知比较好,建议使用助手工具而不是以下的方法。

Tools-->Macro-->输入Macro Name-->Edit-->Description-->点击OK,删除刚才生成的代码,贴代码:

'多行注释
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
关闭并保存。
Tools-->Customize-->Commands-->在Category中选择Marcos-->在右侧Commands中拖出刚才生成的两个宏名到工具条上,-->分别选择Images图标。
设置快捷键:Keyboard-->Category中选择Macros,在Commands中选择一个然后设置快捷键即可。

 

posted on 2013-10-14 19:32  中子持心  阅读(700)  评论(0编辑  收藏  举报

导航