联系方式:
==========================================================================
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,
在无文档打开时调用宏会报错,作者添加了判断语句,屏蔽了这个问题。