转载自: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. line-height下的dispay:inline-block垂直居中

    html: <div class="search fl"> <span class="search-box"> <input ty ...

  2. java 编码转换

    在网络中爬取到的数据,编码可能与当前编译器的编码不相同,而导致可能产生显示乱码的问题.那么如何将网络的编码,转换为当前编译器认可的编码(一般为UTF-8),就是个问题了. 主要使用了两个方法: Str ...

  3. LNMP环境出现”504 gateway time-out”错误的解决方案

    今天下午在调试测试VPS机器的时候,出现"504 gateway time-out"错误提示.VPS是安装的LNMP一键安装包,关于这个错误之前还没有见过,网上看到比较多的是502 ...

  4. git:hook declined FATAL: W refs/heads DENIED by fallthru error

    hook declined FATAL: W refs/heads DENIED by fallthru error git提交代码时报错,网上查了,最终结果竟然是测试人员没有给我配置写的权限,配置了 ...

  5. iOS学习之自定义视图时,在屏幕发生旋转时触发重新布局方法

    如果要对自定义的视图在屏幕旋转时重新布局,则在自定义视图中定义以下触发方法: -(void)layoutSubviews { [super layoutSubviews]; //1.获取到屏幕旋转的方 ...

  6. gtest编译小结(ubuntu 12.10 , gtest 1.6.0)

    1 下载源码,解压之当前用户的主目录(~/) 2 进入make目录,执行make命令 cd ~/gtest-/make make 3 在ubuntu里编译出错,提示找不到lthread库.修改Make ...

  7. Linux iostat监测IO状态(转)

    Linux iostat监测IO状态 2010-03-1  |  13:13分类:Linux,技术细节  |  标签:Linux  |  53,945 views Linux系统出现了性能问题,一般我 ...

  8. Driver Signing changes in Windows 10

    Driver Signing changes in Windows 10 RATE THIS 1 Apr 2015 1:28 PM  39 Beginning with the release of ...

  9. windows下wchar_t* 转char*

    这个在windows下很常见,常用,留个档. 一般用这个函数: size_t wcstombs( char *mbstr, const wchar_t *wcstr, size_t count ); ...

  10. 实用的VIM配置文件

    VIM配置文件名为.vimrc,默认在用户根目录下,或者在命令模式下输入:version可以获取配置文件路径. 在VIM命令行下输入options,然后回车,可以查看VIM所有的参数选项. 双引号&q ...