Word中怎么设置代码高亮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Word中怎么设置代码高亮相关的知识,希望对你有一定的参考价值。

工具:word2010

通过工具栏的荧光笔可以设置代码高亮。

步骤1:打开word文档

步骤2:如要高亮函数名test,则选中test,点击【荧光笔】。

步骤3:效果如下

参考技术A 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”-“运行”,然后执行就可以了。处理较长代码时需要一定时间,请耐心等待。

注意事项:处理前一定要对原文章进行备份,以免前功尽弃。处理较长代码时需要一定时间,请耐心等待。本回答被提问者采纳

使用Excel书写word,如何使选定字体颜色高亮?

请教一下,我使用Excel VBA功能书写固定格式的word文档, 其中需要对选定的字体标加颜色。字体颜色可以实现,如:使用 myWord.Range(a3 + 8, a3 + 14).Font.Color = vbRed 可以让选定区域的6个字符字体颜色改变为红色。 但是,我想让这几个字高亮却无法实现,使用代码 myWord.Range(a3 + 8, a3 + 14).HighlightColorIndex = wdYellow 不报错,也没有任何反应。 尝试了其他代码,均会报错。 使用什么代码可以让选定的这几个字高亮?谢谢大神了。

参考技术A 先选定文字,在插入里面点击艺术字,然后就可以在里面挑选字体的种类 参考技术B 假设数据在A列:
选A列——菜单栏——格式——条件格式——公式——
=ISNUMBER(A1)
选格式——字体——选颜色——确定——确定。
参考技术C 在word中,你点格式中的边框与底纹,在边框中选择网格,然后选择不同的颜色就可以,还可以自定义,想怎么都可以.你试试.
在excel中你点开格式中单元格格式,点边框,先选择样式和颜色,然后再点左边的外边框等,就可以了.

以上是关于Word中怎么设置代码高亮的主要内容,如果未能解决你的问题,请参考以下文章

word2013中怎样设置代码高亮

怎么实现word或是wps里面的代码高亮

使用Excel书写word,如何使选定字体颜色高亮?

Word文字怎么进行高亮设置

word对空格进行高亮显示标记

怎样修改vim默认的背景颜色