转载自: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++实现代码的语法高亮 分类: C_OHTERS 2013-09-22 10:38 2273人阅读 评论(0) 收藏的更多相关文章

  1. C#中的线程(上)-入门 分类: C# 线程 2015-03-09 10:56 53人阅读 评论(0) 收藏

    1.     概述与概念 C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为"主线 ...

  2. Java获取项目中的路径 分类: Java Game 2014-08-14 10:17 122人阅读 评论(0) 收藏

    在项目中经常需要获取某个文件的路径: 在这里提供一些获取路径的方法.. 1.此种方式获取的路径,是当前类所在的路径: UserDAOTest.class.getResource("UserD ...

  3. 网站通用登录模块代码 分类: ASP.NET 2014-12-06 10:49 615人阅读 评论(0) 收藏

    1.HTML部分:     <form id="form1" runat="server">     <script src=".. ...

  4. 在word中使用notepad++实现代码的语法高亮

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

  5. 认识C++中的临时对象temporary object 分类: C/C++ 2015-05-11 23:20 137人阅读 评论(0) 收藏

    C++中临时对象又称无名对象.临时对象主要出现在如下场景. 1.建立一个没有命名的非堆(non-heap)对象,也就是无名对象时,会产生临时对象. Integer inte= Integer(5); ...

  6. C/C++中const的用法 分类: C/C++ 2015-07-05 00:43 85人阅读 评论(0) 收藏

    const是C语言的关键字,经C++进行扩充,变得功能强大,用法复杂.const用于定义一个常变量(只读变量),当const与指针,引用,函数等结合起来使用时,情况会变得复杂的多.下面将从五个方面总结 ...

  7. iOS纯代码手动适配 分类: ios技术 2015-05-04 17:14 239人阅读 评论(0) 收藏

    首先说下让自己的程序支持iPhone6和6+,第一种使用官方提供的launch screen.xib,这个直接看官方文档即可,这里不再多述:第二种方法是和之前iPhone5的类似,比较简单,为iPho ...

  8. iOS中UITextField 使用全面解析 分类: ios技术 2015-04-10 14:37 153人阅读 评论(0) 收藏

    //初始化textfield并设置位置及大小   UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 13 ...

  9. log4j配置文件及nutch中的日志配置 分类: B1_JAVA 2015-02-17 10:58 483人阅读 评论(0) 收藏

    吐槽几句,log4j的坑啊.... (1)CLASSPATH中不能有多个log4j的版本本,否则有有奇形怪状的NoSuchMethod, NoSuchFiled, NoClassDefineFound ...

随机推荐

  1. Linux 获取上个月的第一秒和上个月的最后一秒

    因为写脚本需求须要获得上个月的第一秒和上个月的最后一秒,查阅了相关资料,并通过自己实践.找到了以下这样的方法能满足要求.在此备注,若有其它好的方法.请留言.本人将不胜感激. 获取上个月的第一秒: da ...

  2. 兔子--ps中的基本工具总结(ps cs5)

    矩形选框工具 椭圆选框工具 单行选框工具 单列选框工具 移动工具 套索工具柜 多边形套索工具 磁性套索工具 魔棒工具 高速选择工具 裁剪工具 切片工具 切片选择工具 吸管工具 颜色取样器工具 标尺工具 ...

  3. 一个令人蛋疼的NDK链接错误

    背景 我们APP的引擎包engine.so.包括了A.B.C三个project.但每次都是源代码形式编译,导致svn上存在多份同样代码拷贝. 很不科学. ..核心的Bproject由我维护.整个SO编 ...

  4. 16.REPL 命令

    转自:http://www.runoob.com/nodejs/nodejs-tutorial.html ctrl + c - 退出当前终端. ctrl + c 按下两次 - 退出 Node REPL ...

  5. 深入了解Linux远程桌面

    本文转载于:http://www.linux521.com/2009/system/201004/11001.html 已发表在<网管员世界>2010年3月杂志             本 ...

  6. 版本管理系统:svn和git

    svn是常用的版本管理系统,解决团队协作开发和版本管理问题, 一.服务器端:是一个文件存储仓库,可以设置用户并管理其访问的权限.主要功能包括 ①设置文件存储路径,是管理文件版本的基础 ②设置用户:可以 ...

  7. Monkey服务器命令

  8. 一个小的考试系统 android 思路

    一个小的考试系统 android 思路 假如有 100 组,每组有4个单选钮,设置超时检测确认后去测结果估分视图去切换,如果还有,就再显示下一组 所有结束就给个总结显示 有超时结束过程加上 提示正确选 ...

  9. Direct2D开发:Direct2D 和 GDI 互操作性概述

    本主题说明如何结合使用 Direct2D 和 GDI(可能为英文网页).有两种方法可以结合使用 Direct2D 和 GDI:您可以将 GDI 内容写入与 Direct2D GDI 兼容的呈现器目标, ...

  10. touch、touchevent-事件总结

    1.废话不多说,直接上代码,如下 package com.example.mygestrue; import android.support.v7.app.ActionBarActivity; imp ...