Sub 程序自动加行号()
Dim nLineNum
Dim sLindNum As String
Dim selRge As Range
'以下变量用于"将注释改为蓝色"
Dim lineProgramRange As Range
Dim commentRange As Range '注释范围
Dim TextLine As String '每一行程序代码(内容)
Dim ProgComment As String '每一行程序代码内的注释文字
Dim RgnStart As Long
Dim RgnEnd As Long
Set selRge=Selection.Range '首先记录Selection
For nLineNum=1 to selRge.Paragraphs.Count '针对每个段落
sLineNum=str(nLineNum) '行号转为文字
sLineNum=LTrim(sLineNum) '去除字符串开头和结尾的空格
For i=1 to (3-Len(sLineNum))
sLineNum="0"+sLineNum
Next i
sLineNum="#" & sLineNum
sLineNum=sLinNum+" "
selRge.Paragraphs(nLineNum).Range.InsertBefore(sLineNum)
'将注释改为蓝色
Set lineProgramRange=selRge.Paragraphs(nLineNum).Range
TextLine=lineProgramRange.Text '取得整行文字
CharPos=InStr(1,TextLine,Chr(39)) '寻找注释起始点
If CharPos<>0 Then
ProgComment=Mid(TextLine,CharPos+1) '切割出注释文字
RgnStart=lineProgramRange.Start
RgnEnd=lineProgramRange.End
lineProgramRange.SetRange Start:=RgnStart+CharPos,
End:=RgnEnd
lineProgramRange.Select
Selection.Font.ColorIndex=wdBlue '令注释为蓝色
End If
Next nLineNum
End Sub