首页 > 其他 > 详细

Word 代码高亮

时间:2017-09-09 22:11:40      阅读:307      评论:0      收藏:0      [点我收藏+]

整理文档比较费事,提供个脚本放在VBA里,使Word 代码高亮的一种方法是改变颜色

 script to high light code In document

Private Function isKeyword(w) As Boolean

    Dim keys As New Collection

    With keys
 
.Add "onStart": .Add "Log": .Add "volatile": .Add "friend"
 .Add "abstract": .Add "long": .Add "while": .Add "if"
 .Add "Activity": .Add "native": .Add "FALSE": .Add "implements"
 .Add "asm": .Add "new": .Add "import"
 .Add "auto": .Add "new?": .Add "enabled": .Add "inlIne"
 .Add "bool":  .Add "android": .Add "instanceof"
 .Add "boolean": .Add "onBind": .Add "receiver": .Add "int"
 .Add "boolean?": .Add "onCreate": .Add "exported": .Add "int?"
 .Add "break": .Add "onDestroy": .Add "filter": .Add "Intent"
 .Add "BroadcastReceiver": .Add "onRebind": .Add "action": .Add "interface"
 .Add "byte": .Add "onUnbind": .Add "category": .Add "isDebug?"
 .Add "case": .Add "package": .Add "application": .Add "synchronized"
 .Add "char": .Add "private": .Add "manifest": .Add "template"
 .Add "class": .Add "protected": .Add "xmlns": .Add "this"
 .Add "class?": .Add "protected?": .Add "version": .Add "throw?"
 .Add "const": .Add "public": .Add "encoding": .Add "transient"
 .Add "ContentProvider": .Add "register": .Add "utf": .Add "typename"
 .Add "continue": .Add "return": .Add "INTERNET": .Add "union"
 .Add "default": .Add "sendOrderBroadcast": .Add "RECEIVE_USER_PRESENT": .Add "unsigned"
 .Add "do": .Add "Service": .Add "WAKE_LOCK": .Add "virtual"
 .Add "double": .Add "short": .Add "READ_PHONE_STATE": .Add "void"
 .Add "else": .Add "signed": .Add "WRITE_EXTERNAL_STORAGE"
 .Add "enum": .Add "static": .Add "READ_EXTERNAL_STORAGE"
 .Add "explicit": .Add "static?": .Add "VIBRATE": .Add "CHANGE_WIFI_STATE"
 .Add "extends": .Add "strictfp": .Add "WRITE_SETTINGS": .Add "CHANGE_NETWORK_STATE"
 .Add "extern": .Add "String?": .Add "ACCESS_NETWORK_STATE": .Add "@"
 .Add "final": .Add "struct": .Add "ACCESS_WIFI_STATE": .Add "super"
 .Add "float": .Add "for": .Add "switch": .Add "typedef": .Add "sizeof"
 .Add "try":  .Add "namespace":  .Add "catch":  .Add "operator"
 .Add "cast":  .Add "NULL": .Add "null": .Add "delete":  .Add "throw"
 .Add "dynamic":  .Add "reinterpret":  .Add "true": .Add "TRUE"
 .Add "pub": .Add "provider": .Add "authorities": .Add "Add": .Add "get": .Add "set"
 .Add "uses": .Add "permission": .Add "allowBackup"
 .Add "grant": .Add "URI": .Add "meta": .Add "data": .Add "false": .Add "string": .Add "integer"
    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

  set the style of selection

Private Function isType(ByVal w As String) As 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 "friend": .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 = "java 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     深绿色。lIne comment 水绿色 wdColorAutomatic wdColorBlueGray

            Selection.Font.Bold = True

        ElseIf isOperator(Trim(w)) = True Then

            Selection.Font.Color = wdColorBrown  鲜绿色。

        ElseIf Trim(w) = "//" Then

            lIne comment

            Selection.MoveEnd 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.MoveEndUntil ("*")

                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

 

 

Word 代码高亮

原文:http://www.cnblogs.com/endv/p/7499332.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!