转载自: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. 【1】Laravel5.1 安装

    1.安装composer http://www.phpcomposer.com/ 这个是中文网址里边有教程,但是由于被墙的缘故,可以通过下边这个链接下载Windows安装包 http://docs.p ...

  2. JS+css滑动菜单简单实现

    JS+css滑动菜单 制作一个简单的滑动菜单,当鼠标指向菜单标题时,滑出二级菜单.移开时二级菜单隐藏.目标很简单,实践时有一些细节需要注意,比如鼠标移向二级菜单的 过程中,二级菜单消失了.还有定位出错 ...

  3. 外部主机连接mysql服务器延时严重问题

    1.原因:由于mysql对连接的客户端进行DNS反向解析 2.禁用dns解析,在 /etc/my.cnf 中加入skip-name-resolve 3.反向解析说明: 所谓反向解析是这样的:mysql ...

  4. LODS LODSB LODSW LODSD 例子【载入串指令】

    http://qwop.iteye.com/blog/1958761 // lodsb.cpp : Defines the entry point for the console applicatio ...

  5. 重启电脑提示Error:no such partition grub rescue

    我的系统是Win7,在使用Ubuntu12.04自带的Wubi.exe安装双系统时,系统提示重新启动计算机,重启后电脑就停留在了黑屏界面并提示: error:no such partition gru ...

  6. Android,监控ContentProvider的数据改变

    有时候应用中需要监听ContentProvider的改变并提供响应,这时候就要利用ContentObserver类了 不管是ContentProvider中实现的,insert,delete,upda ...

  7. 深入浅出Node.js (3) - 异步I/O

    3.1 为什么要异步I/O 3.1.1 用户体验 3.1.2 资源分配 3.2 异步I/O实现现状 3.2.1 异步I/O与非阻塞I/O 3.2.2 理想的非阻塞异步I/O 3.2.3 现实的异步I/ ...

  8. ElasticSearch大批量数据入库

    最近着手处理大批量数据的任务. 现状是这样的,一个数据采集程序承载大批量数据的存储和检索.后期可能需要对大批量数据进行统计. 数据分布情况 13个点定时生成采集结果到4个文件(小文件生成周期是5分钟) ...

  9. 【转】如何在ubuntu12.04设置adb驱动

    原文网址:http://www.xuebuyuan.com/1475698.html 在ubuntu上adb驱动不用像在windows上一样需要额外装,只需要写一个配置文件就可以,下面是设置的步骤: ...

  10. linux驱动面试题2

    1.什么是GPIO? general purpose input/output GPIO是相对于芯片本身而言的,如某个管脚是芯片的GPIO脚,则该脚可作为输入或输出高或低电平使用,当然某个脚具有复用的 ...