Word中怎么设置代码高亮?word中关键字高亮的设置方法
时间:2023-04-11 19:23:24
Word中怎么设置代码高亮?一般编辑器中关键字都可以设置高亮,这样醒目而且重点突出,但是word中去不能讲关键字凸显出来,该怎么办呢?下面分享word中关键代码设置高亮的方法
有时写文档时需要将代码粘贴到word中,但直接粘贴到word中的代码虽能保持换行与缩进等格式,但在一般代码编辑工具中的关键字高亮功能却无法实现。该方法无需任何插件,只需要制作一个宏即可实现类似sublime的显示效果
1、首先完成当前所有内容,建议把代码放入单独的文本框中,方便处理,也有较好的视觉效果。记得一定要拷贝一份副本!因为很可能因为代码量比较多,处理宏的时候卡死,只能强制退出WORD了。
2、当前文档新定义一个样式,命名为"code",专门用来对代码进行格式化。由于是代码,所以推荐中文使用黑体(注释等),而英文使用等宽字体(courier new)。步骤如图。
3、选中代码,单击样式库 ccode,将代码应用该样式
4、新建宏,步骤如图
5、将VBA代码(在下一步中)拷贝进去,保存后关闭 (有VBA或相关程序经验者可根据自己需要进行相关修改,如关键词和高亮颜色等)
复制内容到剪贴板
'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 "elseif": .Add "case": .Add "switch": .Add "break"
.Add "for": .Add "continue": .Add "do": .Add "while": .Add "foreach": .Add "echo"
.Add "define": .Add "array": .Add "NULL": .Add "function": .Add "include": .Add "return"
.Add "global": .Add "as": .Add "die": .Add "header": .Add "this": .Add "empty"
.Add "isset": .Add "mysql_fetch_assoc": .Add "class": .Add "style"
.Add "name": .Add "value": .Add "type": .Add "width": .Add "_POST": .Add "_GET"
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 String) As Boolean
Dim types As New Collection
With types
.Add "SELECT": .Add "FROM": .Add "WHERE": .Add "INSERT": .Add "INTO": .Add "VALUES": .Add "ORDER"
.Add "BY": .Add "LIMIT": .Add "ASC": .Add "DESC": .Add "UPDATE": .Add "DELETE": .Add "COUNT"
.Add "html": .Add "head": .Add "title": .Add "body": .Add "p": .Add "h1": .Add " h2"
.Add "h3": .Add "center": .Add "ul": .Add "ol": .Add "li": .Add "a"
.Add "input": .Add "form": .Add "b"
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 = "ccode"
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.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
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
End Sub
6、选定代码文本,然后执行highlight脚本:“视图”-“宏”- 选中“SyntaxHighlight”-“运行”,然后执行就可以了。处理较长代码时需要一定时间,请耐心等待。
注意事项:处理前一定要对原文章进行备份,以免前功尽弃。处理较长代码时需要一定时间,请耐心等待。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Win10专业版无法读取文件或目录损坏怎么处理?
![](https://img.aspxhome.com/file/2023/1/50581_0s.png)
Win10更新与Virtualbox冲突,但从应用管理处找不到该软件怎么办?
![](https://img.aspxhome.com/file/2023/0/51130_0s.png)
Word里面工具栏字体的图标没了,怎么弄?
![](https://img.aspxhome.com/file/2023/7/19027_0s.png)
2招解决Word排版遇到的问题
![](https://img.aspxhome.com/file/2023/4/24114_0s.jpg)
Word打开有的文件但是不显示内容怎么办
word没保存怎么恢复?word未保存如何恢复?
![](https://img.aspxhome.com/file/2023/4/21254_0s.jpg)
Word2007文档种怎么在底部插入Excel表格?
![](https://img.aspxhome.com/file/2023/6/27906_0s.jpg)
Word2003无法输入数字2
Word设置书籍装订式双面打印
word页码如何自动设置
WORD中插入的图片打印出来不清晰
![](https://img.aspxhome.com/file/2023/2/17602_0s.jpg)
win10和win7系统的配置要求如何?win10系统的配置参数简介
![](https://img.aspxhome.com/file/2023/8/46968_0s.png)
Office程序安装的相关问题解答
word设置护眼颜色的两种方法
Word中输入直引号变为弯引号
如何在word2013中删除换行符
![](https://img.aspxhome.com/file/2023/3/19103_0s.jpg)
如何在word插入另一个Word文件的内容
Word应用怎么让Word帮助我学习日语
Word 2007基础:删除自定义库
![](https://img.aspxhome.com/file/2023/9/21589_0s.jpg)
Word文档如何添加行号 Word文档添加行号设置方法
![](https://img.aspxhome.com/file/2023/1/22581_0s.png)