转载自:http://blog.csdn.net/woohello/article/details/7621651

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

 '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 "switch": .Add "case": .Add "default": .Add "break"



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



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



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



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



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



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



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



    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 "void": .Add "struct": .Add "union": .Add "enum": .Add "char": .Add "short": .Add "int"



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



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



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



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



    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 



    wordCount = Selection.Words.Count



    Selection.StartOf wdWord



    While d < wordCount



        d , 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, 



            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, , wdExtend



                Selection.MoveEndUntil ("*")



                Selection.MoveRight wdCharacter, , 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



     To lines



        lIneNum = l & " "



         Then



            lIneNum = lIneNum & " "



        End If



        Selection.Text = lIneNum



        Selection.Font.Bold = False



        Selection.Font.Color = wdColorAutomatic



        p , wdMove)



        Selection.StartOf wdLine



    Next l



End Sub

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

使用方法:

1) 首先为当前文档新定义一个样式,命名为"ccode",专门用来对c代码进行格式化。由于是代码,所以推荐中文使用宋体(注释中),而英文使用等宽字体(courier new)。建立样式的步骤:在word2003中,“格式” → “新样式”

2)将上面的vba代码拷贝到文档中,步骤:在word2003中,“工具” → “宏” → ”VB编辑器“ → ”Normal工程“ → ”Microsoft Word 对象“ ,双击 ”thisDocument"对象,将上面的代码拷贝到新开的窗口中。



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



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



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

在word中使用notepad++实现代码的语法高亮的更多相关文章

  1. 在word中使用notepad++实现代码的语法高亮 分类: C_OHTERS 2013-09-22 10:38 2273人阅读 评论(0) 收藏

    转载自:http://blog.csdn.net/woohello/article/details/7621651 有时写文档时需要将代码粘贴到word中,但直接粘贴到word中的代码虽能保持换行与缩 ...

  2. 在word中显示漂亮的代码

    在word中粘贴或写代码时,通常得不到想要的格式,可用‘Notepad++’工具实现. 步骤: (1)安装Notepad++软件,把代码粘贴进去,选择菜单栏中的语言,然后选择相应代码语言,如P-> ...

  3. 如何在Word中排出漂亮的代码

    引言 学数学和计算机,当然还是用LaTeX排版技术文章更方便.但有时候还是迫不得已需要用Word写作,另外Word其实也有Word的好处,比如细节上的修改要比LaTeX方便. 从Matlab高亮代码复 ...

  4. 用 highlight.js 为文章中的代码添加语法高亮

    来源:http://www.ghostchina.com/adding-syntax-highlighting-to-ghost-using-highlight-js/ --------------- ...

  5. LaTeX中Python代码的语法高亮

    LaTeX中Python代码的语法高亮 本文中,"{}"中的字母为LaTeX或Python的包名,只有"Pygments"是Python的包,其他都是LaTeX ...

  6. MWeb 1.5 发布!增加打字机滚动模式、发布到 Evernote、印象笔记、Wordpress.com、Blogger、编辑器内代码块语法高亮

    打字机滚动模式(Typewriter Scrolling) 快捷键:CMD + Option + T,菜单:View - Typewriter Scrolling ,效果如下图: 发布到 Everno ...

  7. SecureCRT中 secureCRT使用VIM时对语法高亮

    1.在SecureCRT中 secureCRT使用VIM时对语法高亮 其实不是secureCRT的功能,而是VIM的 设置:Options ->Session Options -> Ter ...

  8. 在 Microsoft Word 文档 中粘贴代码实现语法高亮的方法

    1.下载notepad++. 2.将代码粘贴进notepad++,或者直接用notepad++打开. 3.点击顶栏 ===> 插件 ===> NppExport ===> cope ...

  9. Word中的代码怎样语法高亮

    在平常我们粘贴代码到Word中的时候,经常会遇到代码粘贴到Word中后没有语法高亮,看着很乱很不友好,Word自带的样式---语法使用着也不尽人意, 网上有很多做法可以使得在插入在Word中的代码能够 ...

随机推荐

  1. hadoop调优之一:概述

    hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...

  2. Uva220 Othello

     Othello  Othello is a game played by two people on an 8 x 8 board, using disks that are white on on ...

  3. 更改AngularJS的语法解析符号

    // 更改AngularJS的语法解析符号 app.config(function ($interpolateProvider) { $interpolateProvider.startSymbol( ...

  4. c# 输出excel、word实例只需一行代码

    第一.首先下载 WindICFrameNet.dll 下载地址:http://pan.baidu.com/s/1dDBqC9r 第二.前台代码 <div style="display: ...

  5. [原]Sublime Text3 搭建16位汇编环境(windows)

    最近在学习王爽的<汇编程序>,参考<简单OS开发前奏<一>EDITPLUS+MASM32搭建汇编开发环境(16位+32位)>http://www.cnblogs.c ...

  6. BZOJ 2298 problem a(区间DP)

    题意:一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低.”问最少有几个人没有说真话(可能有相同的分数) 思路:考虑最多有多少人说真,那么答案就是n-max. ai个人分数 ...

  7. nginx和tomcat的响应时间

    1.request_time 官网描述:request processing time in seconds with a milliseconds resolution; time elapsed ...

  8. voijs1883 月光的魔法

    背景 影几欺哄了众生了天以外——月儿何曾圆缺 描述 有些东西就如同月光的魔法一般. Luke是爱着vijos的.他想为自己心爱的东西画些什么. 就画N个圆吧.把它们的圆心都固定在x轴上. 圆与圆.为了 ...

  9. hdu 5335 Walk Out(bfs+寻找路径)

    Problem Description In an n∗m maze, the right-bottom corner or a written on it. An explorer gets los ...

  10. 图的遍历(DFS、BFS)

    理论: 深度优先搜索(Depth_Fisrst Search)遍历类似于树的先根遍历,是树的先根遍历的推广: 广度优先搜索(Breadth_First Search) 遍历类似于树的按层次遍历的过程: ...