有时写文档时需要将代码粘贴到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 排版艺术》一书中也有一个为代码添加行号的宏。不知道他的宏和我的宏是否雷同。如有雷同,纯属巧合:)

[转]使用宏,使Word中插入代码高亮-S2C

 ‘script to high light code In document

[转]使用宏,使Word中插入代码高亮-S2C
[转]使用宏,使Word中插入代码高亮-S2C

Private Function isKeyword(w) As Boolean

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    Dim keys As New Collection

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    With keys

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add " if": .Add "else": .Add "switch": .Add "case": .Add "default": .Add "break"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "goto": .Add "return": .Add "for": .Add "while": .Add "do": .Add "continue"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "typedef": .Add "sizeof": .Add "NULL": .Add "new": .Add "delete": .Add "throw"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "try": .Add "catch": .Add "namespace": .Add "operator": .Add "this": .Add "const_cast"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "static_cast": .Add "dynamic_cast": .Add "reinterpret_cast": .Add "true"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "false": .Add "null": .Add "using": .Add "typeid": .Add "and": .Add "and_eq"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "bitand": .Add "bitor": .Add "compl": .Add "not": .Add "not_eq": .Add "or"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "or_eq": .Add "xor": .Add "xor_eq"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    End With

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    isKeyword = isSpecial(w, keys)

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

End Function

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

Private Function isSpecial(ByVal w As String, ByRef col As Collection) As Boolean

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    For Each i In col

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        If w = i Then

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            isSpecial = True

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            Exit Function

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        End If

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    Next

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    isspeical = False

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

End Function

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

Private Function isOperator(w) As Boolean

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    Dim ops As New Collection

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    With ops

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "+": .Add "-": .Add "*": .Add "/": .Add "&": .Add "^": .Add ";"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "%": .Add "#": .Add "!": .Add ":": .Add ",": .Add "."

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "||": .Add "&&": .Add "|": .Add "=": .Add "++": .Add "–"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "’": .Add """"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    End With

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    isOperator = isSpecial(w, ops)

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

End Function

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

Private Function isType(ByVal w As String) As Boolean

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    Dim types As New Collection

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    With types

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "void": .Add "struct": .Add "union": .Add "enum": .Add "char": .Add "short": .Add "int"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "long": .Add "double": .Add "float": .Add "signed": .Add "unsigned": .Add "const": .Add "static"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "extern": .Add "auto": .Add "register": .Add "volatile": .Add "bool": .Add "class": .Add " private"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "protected": .Add "public": .Add "friend": .Add "inlIne": .Add "template": .Add "virtual"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        .Add "asm": .Add "explicit": .Add "typename"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    End With

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    isType = isSpecial(w, types)

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

End Function

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

Sub SyntaxHighlight()

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    Dim wordCount As Integer

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    Dim d As Integer

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    ‘ set the style of selection

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    Selection.Style = "ccode"

[转]使用宏,使Word中插入代码高亮-S2C

    

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    d = 0

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    wordCount = Selection.Words.Count

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    Selection.StartOf wdWord

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    While d < wordCount

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        d = d + Selection.MoveRight(wdWord, 1, wdExtend)

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        w = Selection.Text

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        If isKeyword(Trim(w)) = True Then

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            Selection.Font.Color = wdColorBlue

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        ElseIf isType(Trim(w)) = True Then

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            Selection.Font.Color = wdColorDarkRed

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            Selection.Font.Bold = True

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        ElseIf isOperator(Trim(w)) = True Then

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            Selection.Font.Color = wdColorBrown

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        ElseIf Trim(w) = "//" Then

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            ‘lIne comment

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            Selection.MoveEnd wdLine, 1

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            commentWords = Selection.Words.Count

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            d = d + commentWords

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            Selection.Font.Color = wdColorGreen

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            Selection.MoveStart wdWord, commentWords

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

         ElseIf Trim(w) = "/*" Then

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            ‘block comment

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            While Selection.Characters.Last <> "/"

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

                Selection.MoveLeft wdCharacter, 1, wdExtend

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

                Selection.MoveEndUntil ("*")

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

                Selection.MoveRight wdCharacter, 2, wdExtend

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            Wend

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            commentWords = Selection.Words.Count

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            d = d + commentWords

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            Selection.Font.Color = wdColorGreen

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            Selection.MoveStart wdWord, commentWords

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        End If

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        ‘move the start of selection to next word

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        Selection.MoveStart wdWord

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    Wend

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    ‘ prepare For set lIne number

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    Selection.MoveLeft wdWord, wordCount, wdExtend

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    SetLIneNumber

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

End Sub

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

Private Sub SetLIneNumber()

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    Dim lines As Integer

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    lines = Selection.Paragraphs.Count

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    Selection.StartOf wdParagraph

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    For l = 1 To lines

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        lIneNum = l & " "

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        If l < 10 Then

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

            lIneNum = lIneNum & " "

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        End If

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        Selection.Text = lIneNum

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        Selection.Font.Bold = False

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        Selection.Font.Color = wdColorAutomatic

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        p = Selection.MoveDown(wdLine, 1, wdMove)

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

        Selection.StartOf wdLine

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

    Next l

[转]使用宏,使Word中插入代码高亮-S2C

[转]使用宏,使Word中插入代码高亮-S2C

End Sub

 

下面是我给出的使用说明,原文没给出使用说明。 使用方法:

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

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

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

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

原文:http://blog.chinaunix.net/uid-20672257-id-3323617.html