仿照VC.NET的选择行注释宏

联系方式:cadinfo@263.net

==========================================================================
Sub BackSlashCommentOut()
'DESCRIPTION: Comment several selected rows of codes using double-backslash

'开始定制注释宏
  Dim win
  set win = ActiveWindow
  'Added by cadinfo, 2002,6,1 窗口关闭时无法使用宏,微软有几个自带的宏存在BUG
  if VarType(win)=vbObject Then Exit Sub
  if win.type <> "Text" Then
    MsgBox "This macro can only be run when a text editor window is active."
  else
    'Define three string variable
    TmpBlock = ""
    TmpRow = ""
    CmtBlock = Trim(ActiveDocument.Selection)
    LineNum = ActiveDocument.Selection.CurrentLine
   
    '判断是否为空,空退出宏
   
    if( Len(CmtBlock)=0) Then Exit Sub

    TypeOfFile = FileType(ActiveDocument)
    If TypeOfFile > 0 And TypeOfFile < 5 Then
      If TypeOfFile > 3 Then
        CommentType = "'CMT " ' VBShit
      Else
        CommentType = "//CMT "  'C & C++ & C# &Java use the same
      End If

      '注释方式1 反斜杠backslash "//CMT"

      '---------处理开始----------------
      '直到回车符=0
      Do While Instr (CmtBlock, vbLf) <> 0
        TmpRow = Left(CmtBlock, Instr(CmtBlock, vbLf))
        If Instr(TmpRow, CommentType) = 0 Then  ' 如果没有注释标志,则添加注释
        '添加注释标志"//CMT "
          TmpBlock = TmpBlock + CommentType + TmpRow
        Else             ' 如果有注释标志,则删除注释
          TmpBlock = TmpBlock + Mid (TmpRow, Instr(TmpRow, CommentType)+Len(CommentType), Instr(TmpRow, vbLf))
        End If
        '返回右边的字符串,长度=Len(CmtBlock)-Instr(CmtBlock, vbLf)
        CmtBlock = Right(CmtBlock, (Len(CmtBlock)-Instr(CmtBlock, vbLf)))
      Loop

      '最后一行如果没有选中回车,则在行首添加注释标志"//CMT "
      if(Len(Trim(CmtBlock))<>0) Then
        If Instr(CmtBlock, CommentType) = 0 Then
          CmtBlock=CommentType+CmtBlock
        Else
          CmtBlock = Right (CmtBlock, Len(CmtBlock) - (Instr(CmtBlock, CommentType)+Len(CommentType))+1)
        End If
      End If
     
      CmtBlock = TmpBlock + Trim(CmtBlock)  '拼接字符串
      '---------到此处理完毕----------------

      'ActiveDocument.Selection.Delete
      ActiveDocument.Selection = CmtBlock
      ActiveDocument.Selection.GotoLine LineNum
   
      '添加语句选择处理行   (ActiveDocument.Selection.SelectLine)
'     StartLine = ActiveDocument.Selection.TopLine
'     EndLine = ActiveDocument.Selection.BottomLine
'     For i = StartLine To EndLine
'       ActiveDocument.Selection.GoToLine i, dsSelect
'     Next

      '另外一种注释方式
      'ActiveDocument.Selection = "/*" + ActiveDocument.Selection + "*/"
    Else
      MsgBox("File not supported or unknow error!")
    End If
  End If
'结束定制注释宏
End Sub
========================================================

这次修改,完成了Toogle的功能,并且指出VS自带的一些宏中存在BUG,

在无文档打开时调用宏会报错,作者添加了判断语句,屏蔽了这个问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值