转载自: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. Java系列--第二篇 基于Maven的Android开发HelloAndroidWorld

    曾经写过一篇Android环境配置的随笔,个人感觉特繁琐,既然有Maven,何不尝试用用Maven呢,经网上搜索这篇文章但不限于这些,而做了一个基于Maven的Android版的Hello Andro ...

  2. worklight 中添加时间控件

    在我们使用worklight开发的过程中,由于文档的不开源和插件的缺少,总是自己琢磨很多东东,更有胜者 需要调用源代码实现某些不易实现的功能.在这里把实现的功能代码贴出来,如有不足之处还望指正! 实现 ...

  3. WordPress插件制作教程(八): 最后总结

    WordPress插件教程最后一篇,还是为大家简单的做下总结.这次插件制作教程讲的内容和知识点个人觉得不是很多,因为插件制作不单单是这些内容,它涉及的知识很多很多,不是说你会一些函数就可以做出一个好的 ...

  4. 什么是Code Review

    Code Review 是一种通过复查代码提高代码质量的过程,在XP方法中占有极为重要的地位,也已经成为软件工程中一个不可缺少的环节. 本文通过对Code Review的一些概念和经验的探讨,就如何进 ...

  5. Database JDBC Developer's Guide

    http://docs.oracle.com/database/121/JJDBC/datacc.htm#JJDBC28363

  6. BZOJ 1502 月下柠檬树(simpson积分)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1502 题意:给出如下一棵分层的树,给出每层的高度和每个面的半径.光线是平行的,与地面夹角 ...

  7. Network Attack

    Network Attack Nicola regularly inspects the local networks for security issues. He uses a smart and ...

  8. Java内存回收(垃圾回收)机制总结

    一.背景: Java程序员编写程序时,对于新建的对象,当不再需要此对象时,不必去释放这个对象所占用的空间,这个工作是由Java虚拟机自己完成的 ,即内存回收或垃圾回收. 二.如何知道一个对象所占用的空 ...

  9. CSS3 新特性 开放字体格式WOFF

    疑问 上面这是虾米玩意?    \e806 是在自定义字体表中的字体位置.    好嘛 现在问题来了 WOFF里面是什么东西呢? 怎么才能看到? 用这个:FontCreatorPortable     ...

  10. HDU 1576 A/B(数论)

    题目:求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1).数据给出n和b 推导过程 A/B = K K = 9973* ...