Word中的代码怎样语法高亮
阅读原文时间:2021年09月21日阅读:9

在平常我们粘贴代码到Word中的时候,经常会遇到代码粘贴到Word中后没有语法高亮,看着很乱很不友好,Word自带的样式---语法使用着也不尽人意,

网上有很多做法可以使得在插入在Word中的代码能够高亮显示,有使用notepad++先编辑再复制到Word中的,

也有使用网上的代码复制到转换网站,然后再复制到Word实现代码高亮,也有使用Word插件的,

但是这些方法个人认为多次转换复制太过麻烦,Word插件用着自己又觉得不太放心,故,这里介绍使用Word宏来实现语法高亮

此宏的代码一部分来自互联网,原作者不是太清楚,在这里感谢提供基础源码的作者,这里附上链接https://www.xuebuyuan.com/1843358.html

建议把这个宏做成模板,需要使用的时候引入模板即可,此处我做好的模板可以在这里下载 template

Linux 下可用命令的高亮支持这里只做了一部分的命令,如果要支持所有的Linux可用命令,请自行添加。

如果要添加关键字支持Linux下所有命令的高亮显示,请注意

  1、添加命令的关键字在VB集合中时要注意,定义在单个函数中用 :Add "keyword" 添加的关键字不宜过多,否则在执行宏时会报“执行过程过长”的错误

  2、解决的办法是用多个函数来定义关键字集合,然后再在高亮显示的执行函数中做处理即可。

为了让代码看起来更加整洁,这里我们将代码写在1 X 1 的表格中,原始效果如图:

接下来使用宏来进行语法着色,效果如下:

以下贴出源代码,供大家参考,此代码一部分来自互联网,原作者不是太清楚,在这里感谢提供基础源码的作者,这里附上链接 https://www.xuebuyuan.com/1843358.html

以下是我个人二次开发的源代码,还有一些不足的地方,甚至一些小BUG(个人对VB不是很了解),只能实现上述一些基本功能,希望大家能够继续改进,讨论交流和评论

有兴趣的也可以进行二次开发,实现自己需要的功能。

1 '宏在2019 年 8 月 2 日创建
2 ' 作者:ZhongYuanMei
3
4 '设置行号
5 Private Sub SetLIneNumber()
6
7 Dim lines As Integer
8
9 lines = Selection.Paragraphs.Count
10
11 Selection.StartOf wdParagraph
12
13 For l = 1 To lines
14
15 lIneNum = l & " "
16
17 If l < 10 Then 18 19 lIneNum = lIneNum & " " 20 21 End If 22 23 Selection.Text = lIneNum 24 25 Selection.Font.Bold = False 26 27 Selection.Font.Color = wdColorAutomatic 28 29 p = Selection.MoveDown(wdLine, 1, wdMove) 30 31 Selection.StartOf wdLine 32 33 Next 34 35 End Sub 36 ' 处理集合 37 Private Function isSpecial(ByVal w As String, ByRef col As Collection) As Boolean 38 39 For Each i In col 40 41 If w = i Then 42 43 isSpecial = True 44 45 Exit Function 46 47 End If 48 49 Next 50 51 isspeical = False 52 53 End Function 54 '其它符号 55 Private Function isOthers(ByVal w As String) As Boolean 56 57 Dim others As New Collection 58 59 With others 60 61 .Add "--": .Add "error": .Add "$": .Add """": .Add "{": .Add "}": .Add "[": .Add "]": .Add "root": .Add "@": .Add "(": .Add ")": .Add "()" 62 63 64 End With 65 66 isOthers = isSpecial(w, others) 67 68 End Function 69 '常见的操作运算符 70 Private Function isOperator(w) As Boolean 71 72 Dim ops As New Collection 73 74 With ops 75 76 .Add "+": .Add "-": .Add "*": .Add "/": .Add "&": .Add "^": .Add ";" 77 78 .Add "%": .Add "!": .Add ":": .Add ",": .Add "." 79 80 .Add "||": .Add "&&": .Add "|": .Add "=": .Add "++": .Add "--" 81 82 .Add "'": .Add """": .Add "===": .Add "==": .Add "**": .Add "+=": .Add "-=": .Add "*=": .Add "/=": .Add "%=" 83 84 85 End With 86 87 isOperator = isSpecial(w, ops) 88 89 End Function 90 91 'C语言关键字 92 Private Function isCLANGKeyword(w) As Boolean 93 94 Dim keys As New Collection 95 96 With keys 97 98 .Add "if": .Add "else": .Add "elseif": .Add "case": .Add "switch": .Add "break" 99 100 .Add "for": .Add "continue": .Add "do": .Add "while": .Add "foreach": .Add "echo" 101 102 .Add "define": .Add "array": .Add "NULL": .Add "function": .Add "include": .Add "return" 103 104 .Add "global": .Add "as": .Add "die": .Add "header": .Add "this": .Add "empty" 105 106 .Add "isset": .Add "mysql_fetch_assoc": .Add "class": .Add "style" 107 108 .Add "name": .Add "value": .Add "type": .Add "width": .Add "_POST": .Add "_GET" 109 110 End With 111 112 isCLANGKeyword = isSpecial(w, keys) 113 114 End Function 115 'MySQL关键字 116 Private Function isMySQLKeyword(w) As Boolean 117 118 Dim keys As New Collection 119 120 With keys 121 .Add "ADD": .Add "ALL": .Add "ALTER": .Add "ANALYZE": .Add "AND": .Add "AS": .Add "ASC": .Add "ASENSITIVE": .Add "BEFORE": .Add "BETWEEN" 122 .Add "BIGINT": .Add "BINARY": .Add "BLOB": .Add "BOTH": .Add "BY": .Add "CALL": .Add "CASCADE": .Add "CASE": .Add "CHANGE" 123 .Add "CHAR": .Add "CHARACTER": .Add "CHECK": .Add "COLLATE": .Add "COLUMN": .Add "CONDITION": .Add "CONNECTION" 124 .Add "CONSTRAINT": .Add "CONTINUE": .Add "CONVERT": .Add "CREATE": .Add "CROSS": .Add "CURRENT_DATE": .Add "CURRENT_TIME" 125 .Add "CURRENT_TIMESTAMP": .Add "CURRENT_USER": .Add "CURSOR": .Add "DATABASE": .Add "DATABASES": .Add "DAY_HOUR" 126 .Add "DAY_MICROSECOND": .Add "DAY_MINUTE": .Add "DAY_SECOND": .Add "DEC": .Add "DECIMAL": .Add "DECLARE": .Add "DEFAULT" 127 .Add "DELAYED": .Add "DELETE": .Add "DESC": .Add "DESCRIBE": .Add "DETERMINISTIC": .Add "DISTINCT": .Add "DISTINCTROW" 128 .Add "DIV": .Add "DOUBLE": .Add "DROP": .Add "DUAL": .Add "EACH": .Add "ELSE": .Add "ELSEIF": .Add "ENCLOSED": .Add "ESCAPED" 129 .Add "EXISTS": .Add "EXIT": .Add "EXPLAIN": .Add "FALSE": .Add "FETCH": .Add "FLOAT": .Add "FLOAT4": .Add "FLOAT8": .Add "FOR" 130 .Add "FORCE": .Add "FOREIGN": .Add "FROM": .Add "FULLTEXT": .Add "GOTO": .Add "GRANT": .Add "GROUP": .Add "HAVING" 131 .Add "HIGH_PRIORITY": .Add "HOUR_MICROSECOND": .Add "HOUR_MINUTE": .Add "HOUR_SECOND": .Add "IF": .Add "IGNORE" 132 .Add "IN": .Add "INDEX": .Add "INFILE": .Add "INNER": .Add "INOUT": .Add "INSENSITIVE": .Add "INSERT": .Add "INT": .Add "INT1" 133 .Add "INT2": .Add "INT3": .Add "INT4": .Add "INT8": .Add "INTEGER": .Add "INTERVAL": .Add "INTO""": .Add "IS": .Add "ITERATE" 134 .Add "JOIN": .Add "KEY": .Add "KEYS": .Add "KILL": .Add "LABEL": .Add "LEADING": .Add "LEAVE": .Add "LEFT": .Add "LIKE" 135 .Add "LIMIT": .Add "LINEAR": .Add "LINES": .Add "LOAD": .Add "LOCALTIME": .Add "LOCALTIMESTAMP": .Add "LOCK": .Add "LONG" 136 .Add "LONGBLOB": .Add "LONGTEXT": .Add "LOOP": .Add "LOW_PRIORITY": .Add "MATCH": .Add "MEDIUMBLOB": .Add "MEDIUMINT" 137 .Add "MEDIUMTEXT": .Add "MIDDLEINT": .Add "MINUTE_MICROSECOND": .Add "MINUTE_SECOND": .Add "MOD": .Add "MODIFIES" 138 .Add "NATURAL": .Add "NOT": .Add "NO_WRITE_TO_BINLOG": .Add "NULL": .Add "NUMERIC": .Add "ON": .Add "OPTIMIZE" 139 .Add "OPTION": .Add "OPTIONALLY": .Add "OR": .Add "ORDER": .Add "OUT": .Add "OUTER": .Add "OUTFILE": .Add "PRECISION" 140 .Add "PRIMARY": .Add "PROCEDURE": .Add "PURGE": .Add "RAID0": .Add "RANGE": .Add "READ": .Add "READS": .Add "REAL" 141 .Add "REFERENCES": .Add "REGEXP": .Add "RELEASE": .Add "RENAME": .Add "REPEAT": .Add "REPLACE": .Add "REQUIRE" 142 .Add "RESTRICT": .Add "RETURN": .Add "REVOKE": .Add "RIGHT": .Add "RLIKE": .Add "SCHEMA": .Add "SCHEMAS" 143 .Add "SECOND_MICROSECOND": .Add "SELECT": .Add "SENSITIVE": .Add "SEPARATOR": .Add "SET": .Add "SHOW": .Add "SMALLINT" 144 .Add "SPATIAL": .Add "SPECIFIC": .Add "SQL": .Add "SQLEXCEPTION": .Add "SQLSTATE": .Add "SQLWARNING": .Add "SQL_BIG_RESULT" 145 .Add "SQL_CALC_FOUND_ROWS": .Add "SQL_SMALL_RESULT": .Add "SSL": .Add "STARTING": .Add "STRAIGHT_JOIN": .Add "TABLE" 146 .Add "TERMINATED": .Add "THEN": .Add "TINYBLOB": .Add "TINYINT": .Add "TINYTEXT": .Add "TO": .Add "TRAILING": .Add "TRIGGER" 147 .Add "TRUE": .Add "UNDO": .Add "UNION": .Add "UNIQUE": .Add "UNLOCK": .Add "UNSIGNED": .Add "UPDATE": .Add "USAGE": .Add "USE" 148 .Add "USING": .Add "UTC_DATE": .Add "UTC_TIME": .Add "UTC_TIMESTAMP": .Add "VALUES": .Add "VARBINARY": .Add "VARCHAR" 149 .Add "VARCHARACTER": .Add "VARYING": .Add "WHEN": .Add "WHERE": .Add "WHILE": .Add "WITH": .Add "WRITE": .Add "X509" 150 .Add "XOR": .Add "YEAR_MONTH": .Add "add": .Add "all": .Add "alter": .Add "analyze": .Add "and": .Add "as": .Add "asc" 151 .Add "asensitive": .Add "before": .Add "between": .Add "bigint": .Add "binary": .Add "blob": .Add "both": .Add "by" 152 .Add "call": .Add "cascade": .Add "case": .Add "change": .Add "char": .Add "character": .Add "check": .Add "collate" 153 .Add "column": .Add "condition": .Add "connection": .Add "constraint": .Add "continue": .Add "convert": .Add "create" 154 .Add "cross": .Add "current_date": .Add "current_time": .Add "current_timestamp": .Add "current_user": .Add "cursor" 155 .Add "database": .Add "databases": .Add "day_hour": .Add "day_microsecond": .Add "day_minute": .Add "day_second" 156 .Add "dec": .Add "decimal": .Add "declare": .Add "default": .Add "delayed": .Add "delete": .Add "desc": .Add "describe" 157 .Add "deterministic": .Add "distinct": .Add "distinctrow": .Add "div": .Add "double": .Add "drop": .Add "dual": .Add "each" 158 .Add "else": .Add "elseif": .Add "enclosed": .Add "escaped": .Add "exists": .Add "exit": .Add "explain": .Add "false" 159 .Add "fetch": .Add "float": .Add "float4": .Add "float8": .Add "for": .Add "force": .Add "foreign": .Add "from" 160 .Add "fulltext": .Add "goto": .Add "grant": .Add "group": .Add "having": .Add "high_priority": .Add "hour_microsecond" 161 .Add "hour_minute": .Add "hour_second": .Add "if": .Add "ignore": .Add "in": .Add "index": .Add "infile": .Add "inner" 162 .Add "inout": .Add "insensitive": .Add "insert": .Add "int": .Add "int1": .Add "int2": .Add "int3": .Add "int4": .Add "int8" 163 .Add "integer": .Add "interval": .Add "into""": .Add "is": .Add "iterate": .Add "join": .Add "key": .Add "keys": .Add "kill" 164 .Add "label": .Add "leading": .Add "leave": .Add "left": .Add "like": .Add "limit": .Add "linear": .Add "lines": .Add "load" 165 .Add "localtime": .Add "localtimestamp": .Add "lock": .Add "long": .Add "longblob": .Add "longtext": .Add "loop" 166 .Add "low_priority": .Add "match": .Add "mediumblob": .Add "mediumint": .Add "mediumtext": .Add "middleint" 167 .Add "minute_microsecond": .Add "minute_second": .Add "mod": .Add "modifies": .Add "natural": .Add "not" 168 .Add "no_write_to_binlog": .Add "null": .Add "numeric": .Add "on": .Add "optimize": .Add "option": .Add "optionally" 169 .Add "or": .Add "order": .Add "out": .Add "outer": .Add "outfile": .Add "precision": .Add "primary": .Add "procedure" 170 .Add "purge": .Add "raid0": .Add "range": .Add "read": .Add "reads": .Add "real": .Add "references": .Add "regexp" 171 .Add "release": .Add "rename": .Add "repeat": .Add "replace": .Add "require": .Add "restrict": .Add "return" 172 .Add "revoke": .Add "right": .Add "rlike": .Add "schema": .Add "schemas": .Add "second_microsecond": .Add "select" 173 .Add "sensitive": .Add "separator": .Add "set": .Add "show": .Add "smallint": .Add "spatial": .Add "specific": .Add "sql" 174 .Add "sqlexception": .Add "sqlstate": .Add "sqlwarning": .Add "sql_big_result": .Add "sql_calc_found_rows" 175 .Add "sql_small_result": .Add "ssl": .Add "starting": .Add "straight_join": .Add "table": .Add "terminated" 176 .Add "then": .Add "tinyblob": .Add "tinyint": .Add "tinytext": .Add "to": .Add "trailing": .Add "trigger": .Add "true" 177 .Add "undo": .Add "union": .Add "unique": .Add "unlock": .Add "unsigned": .Add "update": .Add "usage": .Add "use" 178 .Add "using": .Add "utc_date": .Add "utc_time": .Add "utc_timestamp": .Add "values": .Add "varbinary": .Add "varchar" 179 .Add "varcharacter": .Add "varying": .Add "when": .Add "where": .Add "while": .Add "with": .Add "write": .Add "x509" 180 .Add "xor": .Add "year_month" 181 End With 182 183 isMySQLKeyword = isSpecial(w, keys) 184 185 End Function 186 187 'Python语言的关键字 188 Private Function isPythonLANGKeyword(w) As Boolean 189 190 Dim keys As New Collection 191 192 With keys 193 .Add "False": .Add "def": .Add "if": .Add "raise": .Add "None": .Add "del": .Add "import": .Add "return": .Add "True": .Add "elif": .Add "in" 194 .Add "try": .Add "and": .Add "else": .Add "is": .Add "while": .Add "as": .Add "except": .Add "lambda": .Add "with": .Add "assert": .Add "finally" 195 .Add "nonlocal": .Add "yield": .Add "break": .Add "for": .Add "not": .Add "class": .Add "from": .Add "or": .Add "continue": .Add "global": .Add "pass": .Add "self" 196 197 End With 198 199 isPythonLANGKeyword = isSpecial(w, keys) 200 201 End Function 202 203 'Java语言关键字 204 Private Function isJavaLANGKeyword(w) As Boolean 205 206 Dim keys As New Collection 207 208 With keys 209 .Add "abstract": .Add "asm": .Add "auto": .Add "bool": .Add "boolean": .Add "break": .Add "byte": .Add "case": .Add "cast": .Add "catch": .Add "char" 210 .Add "class": .Add "const": .Add "continue": .Add "default": .Add "delete": .Add "do": .Add "double": .Add "dynamic_case": .Add "else": .Add "enum": .Add "explicit": .Add "export": .Add "extern": .Add "extends": .Add "false": .Add "final" 211 .Add "finally": .Add "friend": .Add "float": .Add "for": .Add "goto": .Add "if": .Add "inline": .Add "implements": .Add "import": .Add "instanceof": .Add "inner": .Add "int" 212 .Add "interface": .Add "long": .Add "native": .Add "new": .Add "null": .Add "operator": .Add "package": .Add "private": .Add "protected": .Add "public": .Add "return" 213 .Add "short": .Add "sigend": .Add "static": .Add "static_cast": .Add "struct": .Add "super": .Add "switch": .Add "synchronized": .Add "template": .Add "this": .Add "throw": .Add "throws": .Add "transient": .Add "true" 214 .Add "try": .Add "typedef": .Add "unsigned": .Add "union": .Add "using": .Add "virtual": .Add "void": .Add "volatile": .Add "while": .Add "include": .Add "std" 215 End With 216 217 isJavaLANGKeyword = isSpecial(w, keys) 218 219 End Function 220 'Java语言系统相关的关键字 221 Private Function isJavaSystemLANGKeyword(w) As Boolean 222 223 Dim keys As New Collection 224 225 With keys 226 227 .Add "System": .Add "String": .Add "StringBuffer": .Add "Runnable": .Add "Thread": .Add "Exception": .Add "IOException": .Add "cout": .Add "cin": .Add "std": .Add "endl": .Add "vector" 228 229 End With 230 231 isJavaSystemLANGKeyword = isSpecial(w, keys) 232 233 End Function 234 235 'RHEL系列的系统常用命令 236 Private Function isRHELLANGKeyword(w) As Boolean 237 238 Dim keys As New Collection 239 240 With keys 241 .Add "cat": .Add "chattr": .Add "chgrp": .Add "chmod": .Add "chown": .Add "cksum": .Add "cmp": .Add "diff": .Add "diffstat" 242 .Add "file": .Add "find": .Add "git": .Add "gitview": .Add "indent": .Add "cut": .Add "ln": .Add "less": .Add "locate" 243 .Add "lsattr": .Add "mattrib": .Add "mc": .Add "mdel": .Add "mdir": .Add "mktemp": .Add "more": .Add "mmove": .Add "mread" 244 .Add "mren": .Add "mtools": .Add "mtoolstest": .Add "mv": .Add "od": .Add "paste": .Add "patch": .Add "rcp": .Add "rm": .Add "slocate" 245 .Add "split": .Add "tee": .Add "tmpwatch": .Add "touch": .Add "umask": .Add "which": .Add "cp": .Add "whereis": .Add "mcopy" 246 .Add "mshowfat": .Add "rhmask": .Add "scp ": .Add "awk": .Add "read": .Add "updatedb": .Add "col": .Add "colrm": .Add "comm" 247 .Add "csplit": .Add "ed": .Add "egrep": .Add "ex": .Add "fgrep": .Add "fmt": .Add "fold": .Add "grep": .Add "ispell": .Add "jed" 248 .Add "joe": .Add "join": .Add "look": .Add "mtype": .Add "pico": .Add "rgrep": .Add "sed": .Add "sort": .Add "spell": .Add "tr" 249 .Add "expr": .Add "uniq": .Add "wc": .Add "let": .Add " ": .Add "lprm": .Add "lpr": .Add "lpq": .Add "lpd": .Add "bye": .Add "ftp" 250 .Add "uuto": .Add "uupick": .Add "uucp": .Add "uucico": .Add "tftp": .Add "ncftp": .Add "ftpshut": .Add "ftpwho": .Add "ftpcount" 251 .Add "cd": .Add "df": .Add "dirs": .Add "du": .Add "edquota": .Add "eject": .Add "mcd": .Add "mdeltree": .Add "mdu": .Add "mkdir" 252 .Add "mlabel": .Add "mmd": .Add "mrd": .Add "mzip": .Add "pwd": .Add "quota": .Add "mount": .Add "mmount": .Add "rmdir": .Add "rmt" 253 .Add "stat": .Add "tree": .Add "umount": .Add "ls": .Add "quotacheck": .Add "quotaoff": .Add "lndir": .Add "repquota" 254 .Add "quotaon": .Add "badblocks": .Add "cfdisk": .Add "dd": .Add "e2fsck": .Add "ext2ed": .Add "fsck": .Add "fsck.minix" 255 .Add "fsconf": .Add "fdformat": .Add "hdparm": .Add "mformat": .Add "mkbootdisk": .Add "mkdosfs": .Add "mke2fs": .Add "mkfs.ext2" 256 .Add "mkfs.msdos": .Add "mkinitrd": .Add "mkisofs": .Add "mkswap": .Add "mpartition": .Add "swapon": .Add "symlinks": .Add "sync" 257 .Add "mbadblocks": .Add "mkfs.minix": .Add "fsck.ext2": .Add "fdisk": .Add "losetup": .Add "mkfs": .Add "sfdisk": .Add "swapoff" 258 .Add "apachectl": .Add "arpwatch": .Add "dip": .Add "getty": .Add "mingetty": .Add "uux": .Add "telnet": .Add "uulog": .Add "uustat" 259 .Add "ppp-off": .Add "netconfig": .Add "nc": .Add "httpd": .Add "ifconfig": .Add "minicom": .Add "mesg": .Add "dnsconf": .Add "wall" 260 .Add "netstat": .Add "ping": .Add "pppstats": .Add "samba": .Add "setserial": .Add "talk": .Add "traceroute": .Add "tty" 261 .Add "newaliases": .Add "uuname": .Add "netconf": .Add "write": .Add "statserial": .Add "efax": .Add "pppsetup": .Add "tcpdump" 262 .Add "ytalk": .Add "cu": .Add "smbd": .Add "testparm": .Add "smbclient": .Add "shapecfg": .Add "adduser": .Add "chfn": .Add "useradd" 263 .Add "date": .Add "exit": .Add "finger": .Add "fwhios": .Add "sleep": .Add "suspend": .Add "groupdel": .Add "groupmod": .Add "halt" 264 .Add "kill": .Add "last": .Add "lastb": .Add "login": .Add "logname": .Add "logout": .Add "ps": .Add "nice": .Add "procinfo" 265 .Add "top": .Add "pstree": .Add "reboot": .Add "rlogin": .Add "rsh": .Add "sliplogin": .Add "screen": .Add "shutdown": .Add "rwho" 266 .Add "sudo": .Add "gitps": .Add "swatch": .Add "tload": .Add "logrotate": .Add "uname": .Add "chsh": .Add "userconf": .Add "userdel" 267 .Add "usermod": .Add "vlock": .Add "who": .Add "whoami": .Add "whois": .Add "newgrp": .Add "renice": .Add "su": .Add "skill": .Add "w" 268 .Add "id": .Add "free": .Add "reset": .Add "clear": .Add "alias": .Add "dircolors": .Add "aumix": .Add "bind": .Add "chroot" 269 .Add "clock": .Add "crontab": .Add "declare": .Add "depmod": .Add "dmesg": .Add "enable": .Add "eval": .Add "export" 270 .Add "pwunconv": .Add "grpconv": .Add "rpm": .Add "insmod": .Add "kbdconfig": .Add "lilo": .Add "liloconfig": .Add "lsmod" 271 .Add "minfo": .Add "set": .Add "modprobe": .Add "ntsysv": .Add "mouseconfig": .Add "passwd": .Add "pwconv": .Add "rdate" 272 .Add "resize": .Add "rmmod": .Add "grpunconv": .Add "modinfo": .Add "time": .Add "setup": .Add "sndconfig": .Add "setenv" 273 .Add "setconsole": .Add "timeconfig": .Add "ulimit": .Add "unset": .Add "chkconfig": .Add "apmd": .Add "hwclock" 274 .Add "mkkickstart": .Add "fbset": .Add "unalias": .Add "SVGATextMode": .Add "ar": .Add "bunzip2": .Add "bzip2": .Add "yum" 275 .Add "bzip2recover": .Add "gunzip": .Add "unarj": .Add "compress": .Add "cpio": .Add "dump": .Add "uuencode": .Add "gzexe" 276 .Add "gzip": .Add "lha": .Add "restore": .Add "tar": .Add "uudecode": .Add "unzip": .Add "zip": .Add "zipinfo": .Add "setleds": .Add "echo" 277 .Add "loadkeys": .Add "rdev": .Add "dumpkeys" 278 End With 279 280 isRHELLANGKeyword = isSpecial(w, keys) 281 282 End Function 283 284 'RHEL syntax hight light 285 Sub RHELForSyntaxHighlight() 286 287 Dim wordCount As Integer 288 289 Dim d As Integer 290 291 ' set the style of selection 292 293 ' Selection.Style = "ccode" 294 295 d = 0 296 297 wordCount = Selection.Words.Count 298 299 Selection.StartOf wdWord 300 301 While d < wordCount 302 303 d = d + Selection.MoveRight(wdWord, 1, wdExtend) 304 305 w = Selection.Text 306 If isRHELLANGKeyword(Trim(w)) = True Then 307 308 Selection.Font.Color = wdColorBlue 309 310 Selection.Font.Bold = True 311 312 End If 313 314 If isRHELLANGKeyword(Trim(w)) = True Then 315 316 Selection.Font.Color = wdColorBlue 317 318 Selection.Font.Bold = True 319 320 321 ElseIf isOperator(Trim(w)) = True Then 322 Selection.Font.Bold = True 323 324 Selection.Font.Color = wdColorBrown 325 326 327 ElseIf isOthers(Trim(w)) = True Then 328 329 Selection.Font.Color = wdColorBrown 330 331 Selection.Font.Bold = True 332 333 Else 334 335 Selection.Font.Bold = True 336 End If 337 338 'move the start of selection to next word 339 340 Selection.MoveStart wdWord 341 342 Wend 343 344 ' prepare For set lIne number 345 346 'Selection.MoveLeft wdWord, wordCount, wdExtend 347 348 'SetLIneNumber 349 350 End Sub 351 ' java syntax hight light 352 Sub JavaLANGSyntaxHighlight() 353 354 Dim wordCount As Integer 355 356 Dim d As Integer 357 358 ' set the style of selection 359 360 ' Selection.Style = "ccode" 361 362 d = 0 363 364 wordCount = Selection.Words.Count 365 366 Selection.StartOf wdWord 367 368 While d < wordCount 369 370 d = d + Selection.MoveRight(wdWord, 1, wdExtend) 371 372 w = Selection.Text 373 If isJavaLANGKeyword(Trim(w)) = True Then 374 375 Selection.Font.Color = wdColorRed 376 377 Selection.Font.Bold = True 378 379 ElseIf isJavaSystemLANGKeyword(Trim(w)) = True Then 380 381 Selection.Font.Color = wdColorRed 382 383 Selection.Font.Bold = True 384 385 386 ElseIf isOperator(Trim(w)) = True Then 387 388 Selection.Font.Color = wdColorBrown 389 390 Selection.Font.Bold = True 391 392 ElseIf isOthers(Trim(w)) = True Then 393 394 Selection.Font.Color = wdColorBrown 395 396 Selection.Font.Bold = True 397 398 399 ElseIf Trim(w) = "//" Then 400 401 'lIne comment 402 403 Selection.MoveEnd wdLine, 1 404 405 commentWords = Selection.Words.Count 406 407 d = d + commentWords 408 409 Selection.Font.Color = wdColorBlack 410 411 Selection.MoveStart wdWord, commentWords 412 413 ElseIf Trim(w) = "/*" Then 414 415 'block comment 416 417 While Selection.Characters.Last <> "/"
418
419 Selection.MoveLeft wdCharacter, 1, wdExtend
420
421 Selection.MoveEndUntil ("*")
422
423 Selection.MoveRight wdCharacter, 2, wdExtend
424
425 Wend
426
427 commentWords = Selection.Words.Count
428
429 d = d + commentWords
430
431 Selection.Font.Color = wdColorBlack
432
433 Selection.MoveStart wdWord, commentWords
434
435 Selection.Font.Bold = True
436 Else
437
438 Selection.Font.Bold = True
439
440 End If
441
442 'move the start of selection to next word
443
444 Selection.MoveStart wdWord
445
446 Wend
447
448 ' prepare For set lIne number
449
450 Selection.MoveLeft wdWord, wordCount, wdExtend
451
452 SetLIneNumber
453
454 End Sub
455 ' python syntax high light
456 Sub PythonLANGSyntaxHighlight()
457
458 Dim wordCount As Integer
459
460 Dim d As Integer
461
462 ' set the style of selection
463
464 ' Selection.Style = "ccode"
465
466 d = 0
467
468 wordCount = Selection.Words.Count
469
470 Selection.StartOf wdWord
471
472 While d < wordCount 473 474 d = d + Selection.MoveRight(wdWord, 1, wdExtend) 475 476 w = Selection.Text 477 If isPythonLANGKeyword(Trim(w)) = True Then 478 479 Selection.Font.Color = wdColorViolet 480 481 Selection.Font.Bold = True 482 483 ElseIf isOperator(Trim(w)) = True Then 484 485 Selection.Font.Color = wdColorBrown 486 487 Selection.Font.Bold = True 488 489 ElseIf isOthers(Trim(w)) = True Then 490 491 Selection.Font.Color = wdColorBrown 492 493 Selection.Font.Bold = True 494 495 Else 496 497 Selection.Font.Bold = True 498 End If 499 500 'move the start of selection to next word 501 502 Selection.MoveStart wdWord 503 504 Wend 505 506 ' prepare For set lIne number 507 508 Selection.MoveLeft wdWord, wordCount, wdExtend 509 510 SetLIneNumber 511 512 End Sub 513 'C syntax hight light 514 Sub CLANGSyntaxHighlight() 515 516 Dim wordCount As Integer 517 518 Dim d As Integer 519 520 ' set the style of selection 521 522 ' Selection.Style = "ccode" 523 524 d = 0 525 526 wordCount = Selection.Words.Count 527 528 Selection.StartOf wdWord 529 530 While d < wordCount 531 532 d = d + Selection.MoveRight(wdWord, 1, wdExtend) 533 534 w = Selection.Text 535 If isCLANGKeyword(Trim(w)) = True Then 536 537 Selection.Font.Color = wdColorBlue 538 539 Selection.Font.Bold = True 540 541 542 ElseIf isOperator(Trim(w)) = True Then 543 544 Selection.Font.Bold = True 545 546 Selection.Font.Color = wdColorBrown 547 548 549 ElseIf isOthers(Trim(w)) = True Then 550 551 Selection.Font.Color = wdColorBrown 552 553 Selection.Font.Bold = True 554 555 556 ElseIf Trim(w) = "//" Then 557 558 'lIne comment 559 560 Selection.MoveEnd wdLine, 1 561 562 commentWords = Selection.Words.Count 563 564 d = d + commentWords 565 566 Selection.Font.Color = wdColorBlack 567 568 Selection.MoveStart wdWord, commentWords 569 570 ElseIf Trim(w) = "/*" Then 571 572 'block comment 573 574 While Selection.Characters.Last <> "/"
575
576 Selection.MoveLeft wdCharacter, 1, wdExtend
577
578 Selection.MoveEndUntil ("*")
579
580 Selection.MoveRight wdCharacter, 2, wdExtend
581
582 Wend
583
584 commentWords = Selection.Words.Count
585
586 d = d + commentWords
587
588 Selection.Font.Color = wdColorBlack
589
590 Selection.MoveStart wdWord, commentWords
591
592 Else
593
594 Selection.Font.Bold = True
595 End If
596
597 'move the start of selection to next word
598
599 Selection.MoveStart wdWord
600
601 Wend
602
603 ' prepare For set lIne number
604
605 Selection.MoveLeft wdWord, wordCount, wdExtend
606
607 SetLIneNumber
608
609 End Sub
610
611 ' MySQL syntax hight light
612 Sub MySQLForSyntaxHighlight()
613
614 Dim wordCount As Integer
615
616 Dim d As Integer
617
618 ' set the style of selection
619
620 ' Selection.Style = "ccode"
621
622 d = 0
623
624 wordCount = Selection.Words.Count
625
626 Selection.StartOf wdWord
627
628 While d < wordCount
629
630 d = d + Selection.MoveRight(wdWord, 1, wdExtend)
631
632 w = Selection.Text
633
634 If isMySQLKeyword(Trim(w)) = True Then
635
636 Selection.Font.Color = wdColorRed
637
638 Selection.Font.Bold = True
639
640 ElseIf isOperator(Trim(w)) = True Then
641
642 Selection.Font.Bold = True
643
644 Selection.Font.Color = wdColorBrown
645
646
647 ElseIf isOthers(Trim(w)) = True Then
648
649 Selection.Font.Color = wdColorBrown
650
651 Selection.Font.Bold = True
652
653
654 ElseIf Trim(w) = "#" Then
655
656 'lIne comment
657
658 Selection.MoveEnd wdLine, 1
659
660 commentWords = Selection.Words.Count
661
662 d = d + commentWords
663
664 Selection.Font.Color = wdColorBlack
665
666 Selection.MoveStart wdWord, commentWords
667
668 Else
669
670 Selection.Font.Bold = True
671 End If
672
673 'move the start of selection to next word
674
675 Selection.MoveStart wdWord
676
677 Wend
678
679 ' prepare For set lIne number
680
681 ' Selection.MoveLeft wdWord, wordCount, wdExtend
682
683 ' SetLIneNumber
684
685 End Sub

以下是来自互联网的源代码,原作者不是太清楚,这里附上链接 https://www.xuebuyuan.com/1843358.html

此处再次感谢提供基础源码的作者。

1 'script to high light code In document
2
3 Private Function isKeyword(w) As Boolean
4
5 Dim keys As New Collection
6
7 With keys
8
9 .Add "if": .Add "else": .Add "elseif": .Add "case": .Add "switch": .Add "break"
10
11 .Add "for": .Add "continue": .Add "do": .Add "while": .Add "foreach": .Add "echo"
12
13 .Add "define": .Add "array": .Add "NULL": .Add "function": .Add "include": .Add "return"
14
15 .Add "global": .Add "as": .Add "die": .Add "header": .Add "this": .Add "empty"
16
17 .Add "isset": .Add "mysql_fetch_assoc": .Add "class": .Add "style"
18
19 .Add "name": .Add "value": .Add "type": .Add "width": .Add "_POST": .Add "_GET"
20
21 End With
22
23 isKeyword = isSpecial(w, keys)
24
25 End Function
26
27 Private Function isSpecial(ByVal w As String, ByRef col As Collection) As Boolean
28
29 For Each i In col
30
31 If w = i Then
32
33 isSpecial = True
34
35 Exit Function
36
37 End If
38
39 Next
40
41 isspeical = False
42
43 End Function
44
45 Private Function isOperator(w) As Boolean
46
47 Dim ops As New Collection
48
49 With ops
50
51 .Add "+": .Add "-": .Add "*": .Add "/": .Add "&": .Add "^": .Add ";"
52
53 .Add "%": .Add "#": .Add "!": .Add ":": .Add ",": .Add "."
54
55 .Add "||": .Add "&&": .Add "|": .Add "=": .Add "++": .Add "--"
56
57 .Add "'": .Add """"
58
59 End With
60
61 isOperator = isSpecial(w, ops)
62
63 End Function
64
65 Private Function isType(ByVal w As String) As Boolean
66
67 Dim types As New Collection
68
69 With types
70
71 .Add "SELECT": .Add "FROM": .Add "WHERE": .Add "INSERT": .Add "INTO": .Add "VALUES": .Add "ORDER"
72
73 .Add "BY": .Add "LIMIT": .Add "ASC": .Add "DESC": .Add "UPDATE": .Add "DELETE": .Add "COUNT"
74
75 .Add "html": .Add "head": .Add "title": .Add "body": .Add "p": .Add "h1": .Add " h2"
76
77 .Add "h3": .Add "center": .Add "ul": .Add "ol": .Add "li": .Add "a"
78
79 .Add "input": .Add "form": .Add "b"
80
81 End With
82
83 isType = isSpecial(w, types)
84
85 End Function
86
87 Sub SyntaxHighlight()
88
89 Dim wordCount As Integer
90
91 Dim d As Integer
92
93 ' set the style of selection
94
95 Selection.Style = "ccode"
96
97 d = 0
98
99 wordCount = Selection.Words.Count
100
101 Selection.StartOf wdWord
102
103 While d < wordCount 104 105 d = d + Selection.MoveRight(wdWord, 1, wdExtend) 106 107 w = Selection.Text 108 109 If isKeyword(Trim(w)) = True Then 110 111 Selection.Font.Color = wdColorBlue 112 113 ElseIf isType(Trim(w)) = True Then 114 115 Selection.Font.Color = wdColorDarkRed 116 117 Selection.Font.Bold = True 118 119 ElseIf isOperator(Trim(w)) = True Then 120 121 Selection.Font.Color = wdColorBrown 122 123 ElseIf Trim(w) = "//" Then 124 125 'lIne comment 126 127 Selection.MoveEnd wdLine, 1 128 129 commentWords = Selection.Words.Count 130 131 d = d + commentWords 132 133 Selection.Font.Color = wdColorGreen 134 135 Selection.MoveStart wdWord, commentWords 136 137 ElseIf Trim(w) = "/*" Then 138 139 'block comment 140 141 While Selection.Characters.Last <> "/"
142
143 Selection.MoveLeft wdCharacter, 1, wdExtend
144
145 Selection.MoveEndUntil ("*")
146
147 Selection.MoveRight wdCharacter, 2, wdExtend
148
149 Wend
150
151 commentWords = Selection.Words.Count
152
153 d = d + commentWords
154
155 Selection.Font.Color = wdColorGreen
156
157 Selection.MoveStart wdWord, commentWords
158
159 End If
160
161 'move the start of selection to next word
162
163 Selection.MoveStart wdWord
164
165 Wend
166
167 ' prepare For set lIne number
168
169 Selection.MoveLeft wdWord, wordCount, wdExtend
170
171 SetLIneNumber
172
173 End Sub
174
175 Private Sub SetLIneNumber()
176
177 Dim lines As Integer
178
179 lines = Selection.Paragraphs.Count
180
181 Selection.StartOf wdParagraph
182
183 For l = 1 To lines
184
185 lIneNum = l & " "
186
187 If l < 10 Then
188
189 lIneNum = lIneNum & " "
190
191 End If
192
193 Selection.Text = lIneNum
194
195 Selection.Font.Bold = False
196
197 Selection.Font.Color = wdColorAutomatic
198
199 p = Selection.MoveDown(wdLine, 1, wdMove)
200
201 Selection.StartOf wdLine
202
203 Next
204
205 End Sub

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器