在word中实现代码的语法高亮

来源:互联网 发布:兰州知豆电动车租赁 编辑:程序博客网 时间:2024/05/17 23:11

有时写文档时需要将代码粘贴到word中,但直接粘贴到word中的代码虽能保持换行与缩进等格式,但在一般代码编辑工具中的关键字高亮功能却无法实现,即粘贴到word中的代码不在具有丰富的色彩。使用一款免费软件——notepad++即可实现将关键字高亮的代码粘贴到word中。

首先用notepad++打开源代码文件。notepad++能识别C/C++、Java、matlab等多种语言的源代码。选中要粘贴的代码(如果该代码文件中的所有内容均需要粘贴,则无需选中文字)。然后在选择 插件->NppExport->Copy HTML to clipboard。

然后在word中粘贴即可。

此外,关键字的颜色也可以根据自己的需求在notepad++中进行设置,设置方法:菜单->格式->语言格式设置

——————————–

也可以参考侯捷《word排版艺术》中的vba脚本

由于是代码,所以推荐中文使用宋体(注释中),而英文使用等宽字体(courier new)。

 

————————————-

 

最近我经常在word 里面写东西,发现程序代码拷贝到word 里面就没有了在代码编辑器里面的那种语法高亮的效果,感觉不爽。于是我上网搜了搜,发现目前在word 中实现语法高亮的方法主要是通过安装一个插件。由于我先天的对插件比较反感,所以自己动手,使用word 等office 软件都支持的VBA (Visual BAsic For Application) 写了一个语法高亮的

这个的功能比较简单,就是利用得到文档中选中部分的代码,然后分词,判断该词的类别,然后着色。我现在使用的分词方法是VBA 提供的,大部分情况下和我们预期的比较一致。但是在某些情况下,比如连续的分隔符,这种分词方法会和C 语言分析器的分词结果不同的。

这个宏除了可以语法着色,还可以为代码标注行号。(听说侯捷在《word 排版艺术》一书中也有一个为代码添加行号的宏。不知道他的宏和我的宏是否雷同。如有雷同,纯属巧合:)

 

None 在word中实现代码的语法高亮 ’script to high light code In document
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮Private Function isKeyword(w) As Boolean
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    Dim keys As New Collection
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    With keys
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ” if”: .Add ”else”: .Add ”switch”: .Add ”case”: .Add ”default”: .Add ”break”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”goto”: .Add ”return”: .Add ”for”: .Add ”while”: .Add ”do”: .Add ”continue”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”typedef”: .Add ”sizeof”: .Add ”NULL”: .Add ”new”: .Add ”delete”: .Add ”throw”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”try”: .Add ”catch”: .Add ”namespace”: .Add ”operator”: .Add ”this”: .Add ”const_cast”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”static_cast”: .Add ”dynamic_cast”: .Add ”reinterpret_cast”: .Add ”true”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”false”: .Add ”null”: .Add ”using”: .Add ”typeid”: .Add ”and”: .Add ”and_eq”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”bitand”: .Add ”bitor”: .Add ”compl”: .Add ”not”: .Add ”not_eq”: .Add ”or”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”or_eq”: .Add ”xor”: .Add ”xor_eq”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    End With
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    isKeyword = isSpecial(w, keys)
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮End Function
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮Private Function isSpecial(ByVal w As String, ByRef col As Collection) As Boolean
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    For Each i In col
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        If w = i Then
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            isSpecial = True
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            Exit Function
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        End If
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    Next
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    isspeical = False
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮End Function
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮Private Function isOperator(w) As Boolean
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    Dim ops As New Collection
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    With ops
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”+”: .Add ”-”: .Add ”*”: .Add ”/”: .Add ”&”: .Add ”^”: .Add ”;”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”%”: .Add ”#”: .Add ”!”: .Add ”:”: .Add ”,”: .Add ”.”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”||”: .Add ”&&”: .Add ”|”: .Add ”=”: .Add ”++”: .Add ”–”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”‘”: .Add ”"”"
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    End With
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    isOperator = isSpecial(w, ops)
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮End Function
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮Private Function isType(ByVal w As String) As Boolean
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    Dim types As New Collection
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    With types
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”void”: .Add ”struct”: .Add ”union”: .Add ”enum”: .Add ”char”: .Add ”short”: .Add ”int”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”long”: .Add ”double”: .Add ”float”: .Add ”signed”: .Add ”unsigned”: .Add ”const”: .Add ”static”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”extern”: .Add ”auto”: .Add ”register”: .Add ”volatile”: .Add ”bool”: .Add ”class”: .Add ” private”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”protected”: .Add ”public”: .Add ”friend”: .Add ”inlIne”: .Add ”template”: .Add ”virtual”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        .Add ”asm”: .Add ”explicit”: .Add ”typename”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    End With
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    isType = isSpecial(w, types)
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮End Function
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮Sub SyntaxHighlight()
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    Dim wordCount As Integer
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    Dim d As Integer
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    ’ set the style of selection
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    Selection.Style = ”ccode”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    d = 0
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    wordCount = Selection.Words.Count
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    Selection.StartOf wdWord
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    While d < wordCount
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        d = d + Selection.MoveRight(wdWord, 1, wdExtend)
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        w = Selection.Text
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        If isKeyword(Trim(w)) = True Then
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            Selection.Font.Color = wdColorBlue
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        ElseIf isType(Trim(w)) = True Then
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            Selection.Font.Color = wdColorDarkRed
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            Selection.Font.Bold = True
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        ElseIf isOperator(Trim(w)) = True Then
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            Selection.Font.Color = wdColorBrown
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        ElseIf Trim(w) = ”//” Then
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            ’lIne comment
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            Selection.MoveEnd wdLine, 1
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            commentWords = Selection.Words.Count
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            d = d + commentWords
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            Selection.Font.Color = wdColorGreen
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            Selection.MoveStart wdWord, commentWords
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮         ElseIf Trim(w) = ”/*” Then
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            ’block comment
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            While Selection.Characters.Last <> ”/”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮                Selection.MoveLeft wdCharacter, 1, wdExtend
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮                Selection.MoveEndUntil (“*”)
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮                Selection.MoveRight wdCharacter, 2, wdExtend
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            Wend
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            commentWords = Selection.Words.Count
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            d = d + commentWords
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            Selection.Font.Color = wdColorGreen
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            Selection.MoveStart wdWord, commentWords
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        End If
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        ’move the start of selection to next word
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        Selection.MoveStart wdWord
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    Wend
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    ’ prepare For set lIne number
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    Selection.MoveLeft wdWord, wordCount, wdExtend
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    SetLIneNumber
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮End Sub
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮Private Sub SetLIneNumber()
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    Dim lines As Integer
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    lines = Selection.Paragraphs.Count
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    Selection.StartOf wdParagraph
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    For l = 1 To lines
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        lIneNum = l & ” ”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        If l < 10 Then
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮            lIneNum = lIneNum & ” ”
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        End If
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        Selection.Text = lIneNum
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        Selection.Font.Bold = False
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        Selection.Font.Color = wdColorAutomatic
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        p = Selection.MoveDown(wdLine, 1, wdMove)
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮        Selection.StartOf wdLine
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮    Next l
None 在word中实现代码的语法高亮
None 在word中实现代码的语法高亮End Sub

下面是我给出的使用说明,原文没给出使用说明。 使用方法:
1) 首先为当前文档新定义一个样式,命名为”ccode”,专门用来对c代码进行格式化。由于是代码,所以推荐中文使用宋体(注释中),而英文使用等宽字体(courier new)。建立样式的步骤:在word2003中,“格式” → “新样式”2)将上面的vba代码拷贝到文档中,步骤:在word2003中,“工具” → “宏” → ”VB编辑器“ → ”Normal工程“ → ”Microsoft Word 对象“ ,双击 ”thisDocument”对象,将上面的代码拷贝到新开的窗口中。(word2010默认隐藏了,可以通过以下方式显示:

文件-选项-自定义功能区

自定义功能区的右边选择“主选项卡”,下拉列表中勾选“开发工具”

这样就能在菜单栏上面“开发工具”里看到宏了另外,按alt+F8也可以直接打开宏的对话框

当然你也可以把ccode样式和highlight脚本保存到normal模板中,这样以后你再写代码的时候就可以直接用了,不用自己再辛苦定义这些了。

3)选定代码文本,然后执行highlight脚本: “格式” → “宏” → “宏”, 选择SyntaxHighlight宏,然后执行就可以了。

如果想定制语法高亮,那么修改上面的脚本就是了。

本文出自 齐码代码,转载时请注明出处及相应链接。

本文永久链接: http://www.7mdm.com/801.html

0 0
原创粉丝点击