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”-“运行”,然后执行就可以了。处理较长代码时需要一定时间,请耐心等待。
注意事项:处理前一定要对原文章进行备份,以免前功尽弃。处理较长代码时需要一定时间,请耐心等待。
相关文章
- Word是我们在工作中经常使用的一类办公工具,它可以帮助我们完善文档内容,虽然它比较常见,但是很多的Word技巧我们都是不熟悉的,今天小编就来给大家分享9个实用的Word小2024-10-08
怎么将mathtype安装到word? MathType配置到Word中的详细教程
mathtype是一款比较好用的公式编辑器,它可以内嵌到Word、PPT中,今天我就来给大家讲讲如何把mathtype安装到Word中2024-09-26MathType如何导入word? MathType内容导入word文档的教程
MathType是比较常用的公式编辑器,想要将内容导入word文档中,该怎么倒入呢?下面我们就来看看详细的教程2024-09-26Word格式刷失效无反应怎么办? Word格式刷快捷键失效恢复方法
Word中格式刷的快捷键是Ctrl+Shift+C和Ctrl+Shift+V,分别对应格式复制和格式粘贴,但是发现快捷键不管用下面我们就来看看解决办法2024-09-26- Word 中的对象锚点指示浮动对象(如图像或文本框)相对于文本的位置,该怎么添加锚点呢?下面我们就来看看详细教程2024-09-26
word无法发送内容到PPT怎么办? word发送到ppt功能添加与操作步骤
word怎么发送内容到PPT?我们在word中编辑好内容,却被告知要使用PPT打开,如果手动复制过去很麻烦,那么怎么自动将word内容发送到ppt呢?详细请看下文介绍2024-09-26Word怎么插入三维百分比堆积面积图? Word三维面积图表的用法
Word中经常需要用到三维百分比堆积面积图,那么Word文档中如何插入三维百分比堆积面积图呢?下面我们就来看看详细的教程2024-09-26word怎么插入百分比堆积面积图? word积面积图表制作方法
经常使用word中的图表,想要做一个堆积面积图标,用两种数据百分百填充,该怎么操作呢?详细请看下文介绍2024-09-26- Word中为了一些特殊的目的,要求插入的表格占据页面版心的一定百分比,这种表格该怎样制作呢?详细请看下文介绍2024-09-26
Word怎么给图片添加马赛克气泡效果? 马赛克气泡效果的使用技巧
在word文档中,如果需要对其中的图片效果进行修改的话,应如何操作呢?下面我们就来看看给图片添加马赛克气泡效果的方法2024-09-04
最新评论