Word中高亮程序代码的宏

本文介绍了一种使用VBA实现的代码高亮显示及行号自动添加的方法。通过定义关键字、操作符和类型等元素,实现了不同类型的代码片段在Word文档中以不同颜色突出显示的功能,并为每行代码自动添加了行号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

' script to high light code In document 

Private Function isKeyword(w) As Boolean

    
Dim keys As New Collection

    
With keys

        .Add 
" if": .Add "else": .Add "switch": .Add "cAse": .Add "default": .Add "break"

        .Add 
"goto": .Add "return": .Add "for": .Add "while": .Add "do": .Add "contInue" 

        .Add 
"typedef": .Add "sizeof": .Add "NULL": .Add "new": .Add "delete": .Add "throw" 

        .Add 
"try": .Add "catch": .Add "namespace": .Add "operator": .Add "this": .Add "const_cAst" 

        .Add 
"static_cast": .Add "dynamic_cast": .Add "reInterpret_cAst": .Add "true" 

        .Add 
"false": .Add "null": .Add "usIng": .Add "typeid": .Add "and": .Add "and_eq" 

        .Add 
"bitand": .Add "bitor": .Add "compl": .Add "not": .Add "not_eq": .Add "or" 

        .Add 
"or_eq": .Add "xor": .Add "xor_eq" 

    
End With

    isKeyword 
= isSpecial(w, keys) 

End Function

 

Private Function isSpecial(ByVal w As String, ByRef col As Collection) As Boolean

    
For Each i In col

        
If w = i Then

            isSpecial 
= True

            
Exit Function

        
End If

    
Next

    isspeical 
= False 

End Function

 

Private Function isOperator(w) As Boolean

    
Dim ops As New Collection

    
With ops

        .Add 
"+": .Add "-": .Add "*": .Add "/": .Add "&": .Add "^": .Add ";" 

        .Add 
"%": .Add "#": .Add "!": .Add ":": .Add ",": .Add "." 

        .Add 
"||": .Add "&&": .Add "|": .Add "=": .Add "++": .Add "--" 

        .Add 
"'": .Add """" 

    
End With

    isOperator 
= isSpecial(w, ops) 

End Function

 

Private Function isType(ByVal w As StringAs Boolean

    
Dim types As New Collection

    
With types

        .Add 
"void": .Add "struct": .Add "union": .Add "enum": .Add "char": .Add "short": .Add "Int" 

        .Add 
"long": .Add "double": .Add "float": .Add "signed": .Add "unsigned": .Add "const": .Add "static" 

        .Add 
"extern": .Add "auto": .Add "register": .Add "volatile": .Add "bool": .Add "clAss": .Add " private"

        .Add 
"protected": .Add "public": .Add "fri end": .Add "InlIne": .Add "template": .Add "virtual"

        .Add 
"Asm": .Add "explicit": .Add "typename" 

End With

 isType 
= isSpecial(w, types) 

End Function

Sub SyntaxHighlight()

    
Dim wordCount As Integer

    
Dim d As Integer

    
' set the style of selection 

    Selection.Style 
= "code" 

    

    d 
= 0

    wordCount 
= Selection.Words.Count 

    Selection.StartOf wdWord

    
While d < wordCount

        d 
= d + Selection.MoveRight (wdWord, 1, wdExtend)

        w 
= Selection.Text 

        
If isKeyword(Trim(w)) = True Then

            Selection.Font.Color 
= wdColorBlue

        
ElseIf isType(Trim(w)) = True Then

            Selection.Font.Color 
= wdColorDarkRed

            Selection.Font.Bold 
= True

        
ElseIf isOperator(Trim(w)) = True Then

            Selection.Font.Color 
= wdColorBrown

        
ElseIf Trim(w) = "//" Then

            
'lIne comment 

            Selection.Move 
End wdLIne, 1

            commentWords 
= Selection.Words.Count

            d 
= d + commentWords 

            Selection.Font.Color 
= wdColorGreen

            Selection.MoveStart wdWord, commentWords

         
ElseIf Trim(w) = "/*" Then

            
'block comment

            

            
While Selection.Characters.LAst <> "/"

                Selection.MoveLeft wdCharacter, 
1, wdExtEnd

                Selection.Move EndUntil (
"*")

                Selection.MoveRight wdCharacter, 
2, wdExtEnd

            
Wend

 

            commentWords 
= Selection.Words.Count

            d 
= d + commentWords 

            Selection.Font.Color 
= wdColorGreen

            Selection.MoveStart wdWord, commentWords

        
End If

        
'move the start of selection to next word

        Selection.MoveStart wdWord

    
Wend

    

    
' prepare For set lIne number

    Selection.MoveLeft wdWord, wordCount, wdExtend

    SetLIneNumber 

End Sub

 

Private Sub SetLIneNumber()

    
Dim lines As Integer

    lines 
= Selection.Paragraphs.Count 

    Selection.StartOf wdParagraph

    
For l = 1 To lines

        lIneNum 
= l & " " 

        
If l < 10 Then

            lIneNum 
= lIneNum & " " 

        
End If

        Selection.Text 
= lIneNum

        Selection.Font.Bold 
= False

        Selection.Font.Color 
= wdColorAutomatic

        p 
= Selection.MoveDown (wdLIne, 1, wdMove)

        Selection.StartOf wdLIne

    
Next l

End Sub

 

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值