Code highlighting in word

You can also use SciTE to paste code if you don’t want to install heavy IDEs and then download plugins for all the code you’re making. Simply choose your language from the language menu, type your code, high-light code, select Edit->Copy as RTF, paste into Word with formatting (default paste).

SciTE supports the following languages but probably has support for others: Abaqus*, Ada, ANS.1 MIB definition files*, APDL, Assembler (NASM, MASM), Asymptote*, AutoIt*, Avenue*, Batch files (MS-DOS), Baan*, Bash*, BlitzBasic*, Bullant*, C/C++/C#, Clarion, cmake*, conf (Apache), CSound, CSS*, D, diff files*, E-Script*, Eiffel*, Erlang*, Flagship (Clipper / XBase), Flash (ActionScript), Fortran*, Forth*, GAP*, Gettext, Haskell, HTML*, HTML with embedded JavaScript, VBScript, PHP and ASP*, Gui4Cli*, IDL — both MSIDL and XPIDL*, INI, properties* and similar, InnoSetup*, Java*, JavaScript*, LISP*, LOT*, Lout*, Lua*, Make, Matlab*, Metapost*, MMIXAL, MSSQL, nnCron, NSIS*, Objective Caml*, Opal, Octave*, Pascal/Delphi*, Perl, most of it except for some ambiguous cases*, PL/M*, Progress*, PostScript*, POV-Ray*, PowerBasic*, PowerShell*, PureBasic*, Python*, R*, Rebol*, Ruby*, Scheme*, scriptol*, Specman E*, Spice, Smalltalk, SQL and PLSQL, TADS3*, TeX and LaTeX, Tcl/Tk*, VB and VBScript*, Verilog*, VHDL*, XML*, YAML*.

Microsoft Word is a very versatile software. It has a large number of great features which the common users don’t even know about. Let’s consider a scenario. The other day, Rajesh, a friend of mine sent me an email and told me that he was preparing an MS Word document and needed to embed programming code with syntax highlight. To insert code snippet in his MS Word document, Rajesh did all the syntax highlighting manually. You can imagine that it is a tedious job! So, he asked me if there was an easier way. Well, indeed there is one!

It is easy to embed code snippets with automatic syntax highlight in MS Word. Let’s learn how you can do this magic!

What is a Code Snippet and Syntax Highlighting?

A code snippet is a piece of computer programming code. Such code is usually marked with several colors in order to show various elements of the code distinctively. For example, all the programming language keywords will be highlighted in a particular color and all the errors will be highlighted in a different color. Similarly, there would be per-defined colors for text, tags, properties, attributes, values, symbols etc.

Our tips on MS-Word make your life easier and increase your productivity at work.

Our tips on MS-Word make your life easier and increase your productivity at work.

Program lines in a code snippets are also usually indented to make the code easily readable.

How to Insert Code Snippets with Syntax Highlight in MS Word

First of all, open the MS Word document in which you want to insert the code snippet. Then place the cursor where you want the snippet to be inserted.

Now, go to Insert tab and then click on the Object option. A new box will appear with a list of objects that you can insert in your MS Word document. Select OpenDocument Text from the list and click OK to insert the object.

You will see a new shaded area is inserted at the cursor position. Also a new Word document will open up. Whatever you will write or paste in this new document, the same will appear in the original document.

To add code snippets with syntax highlight, you can use Insert Object method of MS Word.

To add code snippets with syntax highlight, you can use Insert Object method of MS Word.

So, now, copy the code from your code editor and paste it in the new document. Save the new document and that’s it! Code snippet will appear neatly embedded in your MS Word document.

Syntax Highlighting is not Appearing in MS Word

If you have copied the code from a Microsoft product (like MS Visual Studio), the syntax highlight will automatically carry to the MS Word.

However, if you’re using some other code editors (like Programmer’s Notepad), copied code may appear without syntax highlight in MS Word. But don’t panic!

Most of the code editors will give you the option of copying text with HTML markup or RTF format. You should use these options while copying text.

In Programmer’s Notepad, this option appears as Edit -> Copy as RTF

In Notepad++ the option is available as Plugins -> Copy Text with Syntax Highlighting

If you’re copying syntax highlighted code from a webpage, the HTML markups will automatically get copied and syntax highlight will carry to MS Word.

Advantages of Embedding Code Snippets with Syntax Highlights

Although you can always manually highlight various portions of a code snippet in MS Word, but there are many distinctive advantage of using the embedded object method:

  1. Code and main document remain separate from each other (programmers love this approach, don’t they?)
  2. Spelling and grammar mistakes will not be highlighted by MS Word in the embedded code snippet. It is of great help because a lot of programming keywords are not found in dictionary, and as a result such code may result in lots of red and green wavy lines.
  3. And obviously, it’s easier, quicker method. Code remains in it’s original format and syntax highlights.
  4. Formatting changes in the main document do not affect the code block.

So, did you find this MS Word tips useful? Whenever you would need to insert a code snippet with syntax highlight in MS Word document, I am sure it will save you a lot of time. Please feel free to ask any questions on this tutorial. I will try my best to assist you. Thank you for using TechWelkin.

Visualizzare frammenti di codice sorgente in Microsoft Word con Syntax Highlighting

Finding a way to decently show our source code on the web recently became a rather trivial task thanks to many JavaScript + CSS based libraries such as highlightjs, PrismJS, Prettify, SyntaxHighlighter & more. It’s not so easy to achieve the same results inside a text-editing software such as MS Word, to the point that I’ve seen more than few devs adopting the awful & dreadful screenshot method, which consist in blindly capture a portion of their developer framework GUI and then embedding it as an image inside the Word document.

Should we do that? Of course no! There is at least one better way to do that, which also is far less intrusive (and size-intensive) and also gives us the chance to modify/review our code snippet after it has been pasted. Let’s see how we can do it.

  • Open MS Word and place the cursor where you want to paste the snippet.
  • Go to the Insert tab, from there click to the Object… button close to the right-end of the toolbar. A popup will show up: select OpenDocument Text.
  • A new «embedded» document will appear: go to your favourite Developer Framework, such as Visual Studio, Eclipse, Android Studio, Notepad++ or the one doing the highlight the way you want it to be. Copy the code from there and paste it to the new document.
  • Save and close the new document.

Your efforts will be rewarded with something like that:

code-snippet-in-ms-word

This solution, other than being visually nice, grants you the following benefits:

  • the source code will be entirely editable.
  • all the layout, coloring, spaces, tabs, linefeeds from the developer framework will be kept.
  • the code will be treated like a separated, embedded element, just like images and charts, so you’ll be able to move, resize, shape, applying borders, shadows, background color and basically anything else you want to and/or Word allows.
  • the code won’t be affected by grammar checker, meaning that spelling errors won’t be highlighted in the code: no red underlining, yay!
  • it takes only few seconds to do it  — you’ll love that if you need to insert multiple snippets.
  • It works on all Word builds since 2007: Word 2007, Word 2010, Word 2013 and newer versions.

If you find an even better way, please let me know.

Meanwhile… Happy coding!

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it’s also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

When we usually paste code into Word, we often encounter that there is no syntax highlighting after the code is pasted into Word. It looks messy and unfriendly. Word’s own style—grammar is not satisfactory.

There are many ways to highlight the code inserted in Word on the Internet. Some use notepad++ to edit and then copy to Word.

Some use the code on the Internet to copy it to the conversion website, and then copy it to Word to achieve code highlighting, and some use the Word plug-in.

But these methods personally think that it is too troublesome to convert and copy multiple times, and I feel uneasy when using the Word plug-in. Therefore,Here introduces the use of Word macros to achieve syntax highlighting

Part of the code of this macro comes from the Internet. The original author is not too clear. I would like to thank the author who provided the basic source code. Here is a linkhttps://www.xuebuyuan.com/1843358.html

It is recommended to make this macro into a template, and import the template when you need to use it. The template I made here can be downloaded heretemplate

The highlighting of commands available under Linux supports only part of the commands here. If you want to support all the commands available in Linux, please add them yourself.

If you want to add keywords to support the highlighting of all commands under Linux,caution

  1. Pay attention when adding the keyword of the command in the VB collection, the definition is used in a single function:Add «keyword» You should not add too many keywords, otherwise the error «Execution process is too long» will be reported when the macro is executed

  2. The solution is to use multiple functions to define the keyword set, and then do the processing in the highlighted execution function.

In order to make the code look neater, here we write the code in a 1 X 1 table, the original effect is as follows:

Next, use macros for syntax coloring, and the effect is as follows:

The source code is posted below for your reference. Part of this code is from the Internet. The original author is not too clear. Thank you for providing the basic source code. Here is a link.https://www.xuebuyuan.com/1843358.html

The following is the source code of my personal secondary development. There are still some shortcomings, even some small BUGs (I don’t know much about VB), which can only achieve some of the above basic functions. I hope everyone can continue to improve, discuss exchanges and comments

Those who are interested can also carry out secondary development to realize the functions they need.

  1 'Macro created on August 2, 2019
  2 '  Author: ZhongYuanMei
  3 
  4 'Set line number
  5 Private Sub SetLIneNumber()
  6 
  7     Dim lines As Integer
  8 
  9     lines = Selection.Paragraphs.Count
 10 
 11     Selection.StartOf wdParagraph
 12 
 13     For l = 1 To lines
 14 
 15         lIneNum = l & "   "
 16 
 17         If l < 10 Then
 18 
 19             lIneNum = lIneNum & " "
 20 
 21         End If
 22 
 23         Selection.Text = lIneNum
 24 
 25         Selection.Font.Bold = False
 26 
 27         Selection.Font.Color = wdColorAutomatic
 28 
 29         p = Selection.MoveDown(wdLine, 1, wdMove)
 30 
 31         Selection.StartOf wdLine
 32 
 33     Next
 34 
 35 End Sub
 36 '  Processing collection
 37 Private Function isSpecial(ByVal w As String, ByRef col As Collection) As Boolean
 38 
 39     For Each i In col
 40 
 41         If w = i Then
 42 
 43             isSpecial = True
 44 
 45             Exit Function
 46 
 47         End If
 48 
 49     Next
 50 
 51     isspeical = False
 52 
 53 End Function
 54 'Other symbols
 55 Private Function isOthers(ByVal w As String) As Boolean
 56 
 57     Dim others As New Collection
 58 
 59     With others
 60 
 61         .Add "--": .Add "error": .Add "$": .Add """": .Add "{": .Add "}": .Add "[": .Add "]": .Add "root": .Add "@": .Add "(": .Add ")": .Add "()"
 62         
 63 
 64     End With
 65 
 66     isOthers = isSpecial(w, others)
 67 
 68 End Function
 69 'Common operation operators
 70 Private Function isOperator(w) As Boolean
 71 
 72     Dim ops As New Collection
 73 
 74     With ops
 75 
 76         .Add "+": .Add "-": .Add "*": .Add "/": .Add "&": .Add "^": .Add ";"
 77 
 78         .Add "%": .Add "!": .Add ":": .Add ",": .Add "."
 79 
 80         .Add "||": .Add "&&": .Add "|": .Add "=": .Add "++": .Add "--"
 81 
 82         .Add "'": .Add """": .Add "===": .Add "==": .Add "**": .Add "+=": .Add "-=": .Add "*=": .Add "/=": .Add "%="
 83         
 84 
 85     End With
 86 
 87     isOperator = isSpecial(w, ops)
 88 
 89 End Function
 90 
 91 'C language keywords
 92 Private Function isCLANGKeyword(w) As Boolean
 93 
 94     Dim keys As New Collection
 95 
 96     With keys
 97 
 98         .Add "if": .Add "else": .Add "elseif": .Add "case": .Add "switch": .Add "break"
 99 
100         .Add "for": .Add "continue": .Add "do": .Add "while": .Add "foreach": .Add "echo"
101 
102         .Add "define": .Add "array": .Add "NULL": .Add "function": .Add "include": .Add "return"
103 
104         .Add "global": .Add "as": .Add "die": .Add "header": .Add "this": .Add "empty"
105 
106         .Add "isset": .Add "mysql_fetch_assoc": .Add "class": .Add "style"
107 
108         .Add "name": .Add "value": .Add "type": .Add "width": .Add "_POST": .Add "_GET"
109 
110     End With
111 
112     isCLANGKeyword = isSpecial(w, keys)
113 
114 End Function
115 'MySQL keywords
116 Private Function isMySQLKeyword(w) As Boolean
117 
118     Dim keys As New Collection
119 
120     With keys
121         .Add "ADD": .Add "ALL": .Add "ALTER": .Add "ANALYZE": .Add "AND": .Add "AS": .Add "ASC": .Add "ASENSITIVE": .Add "BEFORE": .Add "BETWEEN"
122         .Add "BIGINT": .Add "BINARY": .Add "BLOB": .Add "BOTH": .Add "BY": .Add "CALL": .Add "CASCADE": .Add "CASE": .Add "CHANGE"
123         .Add "CHAR": .Add "CHARACTER": .Add "CHECK": .Add "COLLATE": .Add "COLUMN": .Add "CONDITION": .Add "CONNECTION"
124         .Add "CONSTRAINT": .Add "CONTINUE": .Add "CONVERT": .Add "CREATE": .Add "CROSS": .Add "CURRENT_DATE": .Add "CURRENT_TIME"
125         .Add "CURRENT_TIMESTAMP": .Add "CURRENT_USER": .Add "CURSOR": .Add "DATABASE": .Add "DATABASES": .Add "DAY_HOUR"
126         .Add "DAY_MICROSECOND": .Add "DAY_MINUTE": .Add "DAY_SECOND": .Add "DEC": .Add "DECIMAL": .Add "DECLARE": .Add "DEFAULT"
127         .Add "DELAYED": .Add "DELETE": .Add "DESC": .Add "DESCRIBE": .Add "DETERMINISTIC": .Add "DISTINCT": .Add "DISTINCTROW"
128         .Add "DIV": .Add "DOUBLE": .Add "DROP": .Add "DUAL": .Add "EACH": .Add "ELSE": .Add "ELSEIF": .Add "ENCLOSED": .Add "ESCAPED"
129         .Add "EXISTS": .Add "EXIT": .Add "EXPLAIN": .Add "FALSE": .Add "FETCH": .Add "FLOAT": .Add "FLOAT4": .Add "FLOAT8": .Add "FOR"
130         .Add "FORCE": .Add "FOREIGN": .Add "FROM": .Add "FULLTEXT": .Add "GOTO": .Add "GRANT": .Add "GROUP": .Add "HAVING"
131         .Add "HIGH_PRIORITY": .Add "HOUR_MICROSECOND": .Add "HOUR_MINUTE": .Add "HOUR_SECOND": .Add "IF": .Add "IGNORE"
132         .Add "IN": .Add "INDEX": .Add "INFILE": .Add "INNER": .Add "INOUT": .Add "INSENSITIVE": .Add "INSERT": .Add "INT": .Add "INT1"
133         .Add "INT2": .Add "INT3": .Add "INT4": .Add "INT8": .Add "INTEGER": .Add "INTERVAL": .Add "INTO""": .Add "IS": .Add "ITERATE"
134         .Add "JOIN": .Add "KEY": .Add "KEYS": .Add "KILL": .Add "LABEL": .Add "LEADING": .Add "LEAVE": .Add "LEFT": .Add "LIKE"
135         .Add "LIMIT": .Add "LINEAR": .Add "LINES": .Add "LOAD": .Add "LOCALTIME": .Add "LOCALTIMESTAMP": .Add "LOCK": .Add "LONG"
136         .Add "LONGBLOB": .Add "LONGTEXT": .Add "LOOP": .Add "LOW_PRIORITY": .Add "MATCH": .Add "MEDIUMBLOB": .Add "MEDIUMINT"
137         .Add "MEDIUMTEXT": .Add "MIDDLEINT": .Add "MINUTE_MICROSECOND": .Add "MINUTE_SECOND": .Add "MOD": .Add "MODIFIES"
138         .Add "NATURAL": .Add "NOT": .Add "NO_WRITE_TO_BINLOG": .Add "NULL": .Add "NUMERIC": .Add "ON": .Add "OPTIMIZE"
139         .Add "OPTION": .Add "OPTIONALLY": .Add "OR": .Add "ORDER": .Add "OUT": .Add "OUTER": .Add "OUTFILE": .Add "PRECISION"
140         .Add "PRIMARY": .Add "PROCEDURE": .Add "PURGE": .Add "RAID0": .Add "RANGE": .Add "READ": .Add "READS": .Add "REAL"
141         .Add "REFERENCES": .Add "REGEXP": .Add "RELEASE": .Add "RENAME": .Add "REPEAT": .Add "REPLACE": .Add "REQUIRE"
142         .Add "RESTRICT": .Add "RETURN": .Add "REVOKE": .Add "RIGHT": .Add "RLIKE": .Add "SCHEMA": .Add "SCHEMAS"
143         .Add "SECOND_MICROSECOND": .Add "SELECT": .Add "SENSITIVE": .Add "SEPARATOR": .Add "SET": .Add "SHOW": .Add "SMALLINT"
144         .Add "SPATIAL": .Add "SPECIFIC": .Add "SQL": .Add "SQLEXCEPTION": .Add "SQLSTATE": .Add "SQLWARNING": .Add "SQL_BIG_RESULT"
145         .Add "SQL_CALC_FOUND_ROWS": .Add "SQL_SMALL_RESULT": .Add "SSL": .Add "STARTING": .Add "STRAIGHT_JOIN": .Add "TABLE"
146         .Add "TERMINATED": .Add "THEN": .Add "TINYBLOB": .Add "TINYINT": .Add "TINYTEXT": .Add "TO": .Add "TRAILING": .Add "TRIGGER"
147         .Add "TRUE": .Add "UNDO": .Add "UNION": .Add "UNIQUE": .Add "UNLOCK": .Add "UNSIGNED": .Add "UPDATE": .Add "USAGE": .Add "USE"
148         .Add "USING": .Add "UTC_DATE": .Add "UTC_TIME": .Add "UTC_TIMESTAMP": .Add "VALUES": .Add "VARBINARY": .Add "VARCHAR"
149         .Add "VARCHARACTER": .Add "VARYING": .Add "WHEN": .Add "WHERE": .Add "WHILE": .Add "WITH": .Add "WRITE": .Add "X509"
150         .Add "XOR": .Add "YEAR_MONTH": .Add "add": .Add "all": .Add "alter": .Add "analyze": .Add "and": .Add "as": .Add "asc"
151         .Add "asensitive": .Add "before": .Add "between": .Add "bigint": .Add "binary": .Add "blob": .Add "both": .Add "by"
152         .Add "call": .Add "cascade": .Add "case": .Add "change": .Add "char": .Add "character": .Add "check": .Add "collate"
153         .Add "column": .Add "condition": .Add "connection": .Add "constraint": .Add "continue": .Add "convert": .Add "create"
154         .Add "cross": .Add "current_date": .Add "current_time": .Add "current_timestamp": .Add "current_user": .Add "cursor"
155         .Add "database": .Add "databases": .Add "day_hour": .Add "day_microsecond": .Add "day_minute": .Add "day_second"
156         .Add "dec": .Add "decimal": .Add "declare": .Add "default": .Add "delayed": .Add "delete": .Add "desc": .Add "describe"
157         .Add "deterministic": .Add "distinct": .Add "distinctrow": .Add "div": .Add "double": .Add "drop": .Add "dual": .Add "each"
158         .Add "else": .Add "elseif": .Add "enclosed": .Add "escaped": .Add "exists": .Add "exit": .Add "explain": .Add "false"
159         .Add "fetch": .Add "float": .Add "float4": .Add "float8": .Add "for": .Add "force": .Add "foreign": .Add "from"
160         .Add "fulltext": .Add "goto": .Add "grant": .Add "group": .Add "having": .Add "high_priority": .Add "hour_microsecond"
161         .Add "hour_minute": .Add "hour_second": .Add "if": .Add "ignore": .Add "in": .Add "index": .Add "infile": .Add "inner"
162         .Add "inout": .Add "insensitive": .Add "insert": .Add "int": .Add "int1": .Add "int2": .Add "int3": .Add "int4": .Add "int8"
163         .Add "integer": .Add "interval": .Add "into""": .Add "is": .Add "iterate": .Add "join": .Add "key": .Add "keys": .Add "kill"
164         .Add "label": .Add "leading": .Add "leave": .Add "left": .Add "like": .Add "limit": .Add "linear": .Add "lines": .Add "load"
165         .Add "localtime": .Add "localtimestamp": .Add "lock": .Add "long": .Add "longblob": .Add "longtext": .Add "loop"
166         .Add "low_priority": .Add "match": .Add "mediumblob": .Add "mediumint": .Add "mediumtext": .Add "middleint"
167         .Add "minute_microsecond": .Add "minute_second": .Add "mod": .Add "modifies": .Add "natural": .Add "not"
168         .Add "no_write_to_binlog": .Add "null": .Add "numeric": .Add "on": .Add "optimize": .Add "option": .Add "optionally"
169         .Add "or": .Add "order": .Add "out": .Add "outer": .Add "outfile": .Add "precision": .Add "primary": .Add "procedure"
170         .Add "purge": .Add "raid0": .Add "range": .Add "read": .Add "reads": .Add "real": .Add "references": .Add "regexp"
171         .Add "release": .Add "rename": .Add "repeat": .Add "replace": .Add "require": .Add "restrict": .Add "return"
172         .Add "revoke": .Add "right": .Add "rlike": .Add "schema": .Add "schemas": .Add "second_microsecond": .Add "select"
173         .Add "sensitive": .Add "separator": .Add "set": .Add "show": .Add "smallint": .Add "spatial": .Add "specific": .Add "sql"
174         .Add "sqlexception": .Add "sqlstate": .Add "sqlwarning": .Add "sql_big_result": .Add "sql_calc_found_rows"
175         .Add "sql_small_result": .Add "ssl": .Add "starting": .Add "straight_join": .Add "table": .Add "terminated"
176         .Add "then": .Add "tinyblob": .Add "tinyint": .Add "tinytext": .Add "to": .Add "trailing": .Add "trigger": .Add "true"
177         .Add "undo": .Add "union": .Add "unique": .Add "unlock": .Add "unsigned": .Add "update": .Add "usage": .Add "use"
178         .Add "using": .Add "utc_date": .Add "utc_time": .Add "utc_timestamp": .Add "values": .Add "varbinary": .Add "varchar"
179         .Add "varcharacter": .Add "varying": .Add "when": .Add "where": .Add "while": .Add "with": .Add "write": .Add "x509"
180         .Add "xor": .Add "year_month"
181     End With
182 
183     isMySQLKeyword = isSpecial(w, keys)
184 
185 End Function
186 
187 'Keywords of Python language
188 Private Function isPythonLANGKeyword(w) As Boolean
189 
190     Dim keys As New Collection
191 
192     With keys
193         .Add "False": .Add "def": .Add "if": .Add "raise": .Add "None": .Add "del": .Add "import": .Add "return": .Add "True": .Add "elif": .Add "in"
194         .Add "try": .Add "and": .Add "else": .Add "is": .Add "while": .Add "as": .Add "except": .Add "lambda": .Add "with": .Add "assert": .Add "finally"
195         .Add "nonlocal": .Add "yield": .Add "break": .Add "for": .Add "not": .Add "class": .Add "from": .Add "or": .Add "continue": .Add "global": .Add "pass": .Add "self"
196 
197     End With
198 
199     isPythonLANGKeyword = isSpecial(w, keys)
200 
201 End Function
202 
203 'Java language keywords
204 Private Function isJavaLANGKeyword(w) As Boolean
205 
206     Dim keys As New Collection
207 
208     With keys
209         .Add "abstract": .Add "asm": .Add "auto": .Add "bool": .Add "boolean": .Add "break": .Add "byte": .Add "case": .Add "cast": .Add "catch": .Add "char"
210         .Add "class": .Add "const": .Add "continue": .Add "default": .Add "delete": .Add "do": .Add "double": .Add "dynamic_case": .Add "else": .Add "enum": .Add "explicit": .Add "export": .Add "extern": .Add "extends": .Add "false": .Add "final"
211         .Add "finally": .Add "friend": .Add "float": .Add "for": .Add "goto": .Add "if": .Add "inline": .Add "implements": .Add "import": .Add "instanceof": .Add "inner": .Add "int"
212         .Add "interface": .Add "long": .Add "native": .Add "new": .Add "null": .Add "operator": .Add "package": .Add "private": .Add "protected": .Add "public": .Add "return"
213         .Add "short": .Add "sigend": .Add "static": .Add "static_cast": .Add "struct": .Add "super": .Add "switch": .Add "synchronized": .Add "template": .Add "this": .Add "throw": .Add "throws": .Add "transient": .Add "true"
214         .Add "try": .Add "typedef": .Add "unsigned": .Add "union": .Add "using": .Add "virtual": .Add "void": .Add "volatile": .Add "while": .Add "include": .Add "std"
215     End With
216 
217     isJavaLANGKeyword = isSpecial(w, keys)
218 
219 End Function
220 'Keywords related to the Java language system
221 Private Function isJavaSystemLANGKeyword(w) As Boolean
222 
223     Dim keys As New Collection
224 
225     With keys
226     
227        .Add "System": .Add "String": .Add "StringBuffer": .Add "Runnable": .Add "Thread": .Add "Exception": .Add "IOException": .Add "cout": .Add "cin": .Add "std": .Add "endl": .Add "vector"
228     
229     End With
230 
231     isJavaSystemLANGKeyword = isSpecial(w, keys)
232 
233 End Function
234 
235 'Common commands of RHEL series system
236 Private Function isRHELLANGKeyword(w) As Boolean
237 
238     Dim keys As New Collection
239 
240     With keys
241         .Add "cat": .Add "chattr": .Add "chgrp": .Add "chmod": .Add "chown": .Add "cksum": .Add "cmp": .Add "diff": .Add "diffstat"
242         .Add "file": .Add "find": .Add "git": .Add "gitview": .Add "indent": .Add "cut": .Add "ln": .Add "less": .Add "locate"
243         .Add "lsattr": .Add "mattrib": .Add "mc": .Add "mdel": .Add "mdir": .Add "mktemp": .Add "more": .Add "mmove": .Add "mread"
244         .Add "mren": .Add "mtools": .Add "mtoolstest": .Add "mv": .Add "od": .Add "paste": .Add "patch": .Add "rcp": .Add "rm": .Add "slocate"
245         .Add "split": .Add "tee": .Add "tmpwatch": .Add "touch": .Add "umask": .Add "which": .Add "cp": .Add "whereis": .Add "mcopy"
246         .Add "mshowfat": .Add "rhmask": .Add "scp ": .Add "awk": .Add "read": .Add "updatedb": .Add "col": .Add "colrm": .Add "comm"
247         .Add "csplit": .Add "ed": .Add "egrep": .Add "ex": .Add "fgrep": .Add "fmt": .Add "fold": .Add "grep": .Add "ispell": .Add "jed"
248         .Add "joe": .Add "join": .Add "look": .Add "mtype": .Add "pico": .Add "rgrep": .Add "sed": .Add "sort": .Add "spell": .Add "tr"
249         .Add "expr": .Add "uniq": .Add "wc": .Add "let": .Add " ": .Add "lprm": .Add "lpr": .Add "lpq": .Add "lpd": .Add "bye": .Add "ftp"
250         .Add "uuto": .Add "uupick": .Add "uucp": .Add "uucico": .Add "tftp": .Add "ncftp": .Add "ftpshut": .Add "ftpwho": .Add "ftpcount"
251         .Add "cd": .Add "df": .Add "dirs": .Add "du": .Add "edquota": .Add "eject": .Add "mcd": .Add "mdeltree": .Add "mdu": .Add "mkdir"
252         .Add "mlabel": .Add "mmd": .Add "mrd": .Add "mzip": .Add "pwd": .Add "quota": .Add "mount": .Add "mmount": .Add "rmdir": .Add "rmt"
253         .Add "stat": .Add "tree": .Add "umount": .Add "ls": .Add "quotacheck": .Add "quotaoff": .Add "lndir": .Add "repquota"
254         .Add "quotaon": .Add "badblocks": .Add "cfdisk": .Add "dd": .Add "e2fsck": .Add "ext2ed": .Add "fsck": .Add "fsck.minix"
255         .Add "fsconf": .Add "fdformat": .Add "hdparm": .Add "mformat": .Add "mkbootdisk": .Add "mkdosfs": .Add "mke2fs": .Add "mkfs.ext2"
256         .Add "mkfs.msdos": .Add "mkinitrd": .Add "mkisofs": .Add "mkswap": .Add "mpartition": .Add "swapon": .Add "symlinks": .Add "sync"
257         .Add "mbadblocks": .Add "mkfs.minix": .Add "fsck.ext2": .Add "fdisk": .Add "losetup": .Add "mkfs": .Add "sfdisk": .Add "swapoff"
258         .Add "apachectl": .Add "arpwatch": .Add "dip": .Add "getty": .Add "mingetty": .Add "uux": .Add "telnet": .Add "uulog": .Add "uustat"
259         .Add "ppp-off": .Add "netconfig": .Add "nc": .Add "httpd": .Add "ifconfig": .Add "minicom": .Add "mesg": .Add "dnsconf": .Add "wall"
260         .Add "netstat": .Add "ping": .Add "pppstats": .Add "samba": .Add "setserial": .Add "talk": .Add "traceroute": .Add "tty"
261         .Add "newaliases": .Add "uuname": .Add "netconf": .Add "write": .Add "statserial": .Add "efax": .Add "pppsetup": .Add "tcpdump"
262         .Add "ytalk": .Add "cu": .Add "smbd": .Add "testparm": .Add "smbclient": .Add "shapecfg": .Add "adduser": .Add "chfn": .Add "useradd"
263         .Add "date": .Add "exit": .Add "finger": .Add "fwhios": .Add "sleep": .Add "suspend": .Add "groupdel": .Add "groupmod": .Add "halt"
264         .Add "kill": .Add "last": .Add "lastb": .Add "login": .Add "logname": .Add "logout": .Add "ps": .Add "nice": .Add "procinfo"
265         .Add "top": .Add "pstree": .Add "reboot": .Add "rlogin": .Add "rsh": .Add "sliplogin": .Add "screen": .Add "shutdown": .Add "rwho"
266         .Add "sudo": .Add "gitps": .Add "swatch": .Add "tload": .Add "logrotate": .Add "uname": .Add "chsh": .Add "userconf": .Add "userdel"
267         .Add "usermod": .Add "vlock": .Add "who": .Add "whoami": .Add "whois": .Add "newgrp": .Add "renice": .Add "su": .Add "skill": .Add "w"
268         .Add "id": .Add "free": .Add "reset": .Add "clear": .Add "alias": .Add "dircolors": .Add "aumix": .Add "bind": .Add "chroot"
269         .Add "clock": .Add "crontab": .Add "declare": .Add "depmod": .Add "dmesg": .Add "enable": .Add "eval": .Add "export"
270         .Add "pwunconv": .Add "grpconv": .Add "rpm": .Add "insmod": .Add "kbdconfig": .Add "lilo": .Add "liloconfig": .Add "lsmod"
271         .Add "minfo": .Add "set": .Add "modprobe": .Add "ntsysv": .Add "mouseconfig": .Add "passwd": .Add "pwconv": .Add "rdate"
272         .Add "resize": .Add "rmmod": .Add "grpunconv": .Add "modinfo": .Add "time": .Add "setup": .Add "sndconfig": .Add "setenv"
273         .Add "setconsole": .Add "timeconfig": .Add "ulimit": .Add "unset": .Add "chkconfig": .Add "apmd": .Add "hwclock"
274         .Add "mkkickstart": .Add "fbset": .Add "unalias": .Add "SVGATextMode": .Add "ar": .Add "bunzip2": .Add "bzip2": .Add "yum"
275         .Add "bzip2recover": .Add "gunzip": .Add "unarj": .Add "compress": .Add "cpio": .Add "dump": .Add "uuencode": .Add "gzexe"
276         .Add "gzip": .Add "lha": .Add "restore": .Add "tar": .Add "uudecode": .Add "unzip": .Add "zip": .Add "zipinfo": .Add "setleds": .Add "echo"
277         .Add "loadkeys": .Add "rdev": .Add "dumpkeys"
278     End With
279 
280     isRHELLANGKeyword = isSpecial(w, keys)
281 
282 End Function
283 
284 'RHEL syntax hight light
285 Sub RHELForSyntaxHighlight()
286 
287     Dim wordCount As Integer
288 
289     Dim d As Integer
290 
291     ' set the style of selection
292 
293     ' Selection.Style = "ccode"
294 
295     d = 0
296 
297     wordCount = Selection.Words.Count
298 
299     Selection.StartOf wdWord
300 
301     While d < wordCount
302 
303         d = d + Selection.MoveRight(wdWord, 1, wdExtend)
304 
305         w = Selection.Text
306         If isRHELLANGKeyword(Trim(w)) = True Then
307 
308             Selection.Font.Color = wdColorBlue
309 
310             Selection.Font.Bold = True
311             
312         End If
313             
314         If isRHELLANGKeyword(Trim(w)) = True Then
315 
316             Selection.Font.Color = wdColorBlue
317 
318             Selection.Font.Bold = True
319             
320             
321         ElseIf isOperator(Trim(w)) = True Then
322             Selection.Font.Bold = True
323 
324             Selection.Font.Color = wdColorBrown
325             
326             
327         ElseIf isOthers(Trim(w)) = True Then
328 
329             Selection.Font.Color = wdColorBrown
330 
331             Selection.Font.Bold = True
332             
333         Else
334             
335             Selection.Font.Bold = True
336         End If
337 
338         'move the start of selection to next word
339 
340         Selection.MoveStart wdWord
341 
342     Wend
343 
344     ' prepare For set lIne number
345 
346     'Selection.MoveLeft wdWord, wordCount, wdExtend
347 
348     'SetLIneNumber
349 
350 End Sub
351 ' java syntax hight light
352 Sub JavaLANGSyntaxHighlight()
353 
354     Dim wordCount As Integer
355 
356     Dim d As Integer
357 
358     ' set the style of selection
359 
360     ' Selection.Style = "ccode"
361 
362     d = 0
363 
364     wordCount = Selection.Words.Count
365 
366     Selection.StartOf wdWord
367 
368     While d < wordCount
369 
370         d = d + Selection.MoveRight(wdWord, 1, wdExtend)
371 
372         w = Selection.Text
373         If isJavaLANGKeyword(Trim(w)) = True Then
374 
375             Selection.Font.Color = wdColorRed
376 
377             Selection.Font.Bold = True
378             
379         ElseIf isJavaSystemLANGKeyword(Trim(w)) = True Then
380 
381             Selection.Font.Color = wdColorRed
382 
383             Selection.Font.Bold = True
384             
385             
386         ElseIf isOperator(Trim(w)) = True Then
387 
388             Selection.Font.Color = wdColorBrown
389             
390             Selection.Font.Bold = True
391             
392         ElseIf isOthers(Trim(w)) = True Then
393 
394             Selection.Font.Color = wdColorBrown
395 
396             Selection.Font.Bold = True
397 
398 
399         ElseIf Trim(w) = "//" Then
400 
401             'lIne comment
402 
403             Selection.MoveEnd wdLine, 1
404 
405             commentWords = Selection.Words.Count
406 
407             d = d + commentWords
408 
409             Selection.Font.Color = wdColorBlack
410 
411             Selection.MoveStart wdWord, commentWords
412 
413          ElseIf Trim(w) = "/*" Then
414 
415             'block comment
416 
417             While Selection.Characters.Last <> "/"
418 
419                 Selection.MoveLeft wdCharacter, 1, wdExtend
420 
421                 Selection.MoveEndUntil ("*")
422 
423                 Selection.MoveRight wdCharacter, 2, wdExtend
424 
425             Wend
426 
427             commentWords = Selection.Words.Count
428 
429             d = d + commentWords
430 
431             Selection.Font.Color = wdColorBlack
432             
433             Selection.MoveStart wdWord, commentWords
434             
435             Selection.Font.Bold = True
436         Else
437             
438             Selection.Font.Bold = True
439 
440         End If
441 
442         'move the start of selection to next word
443 
444         Selection.MoveStart wdWord
445 
446     Wend
447 
448     ' prepare For set lIne number
449 
450     Selection.MoveLeft wdWord, wordCount, wdExtend
451 
452     SetLIneNumber
453 
454 End Sub
455 ' python syntax high light
456 Sub PythonLANGSyntaxHighlight()
457 
458     Dim wordCount As Integer
459 
460     Dim d As Integer
461 
462     ' set the style of selection
463 
464     ' Selection.Style = "ccode"
465 
466     d = 0
467 
468     wordCount = Selection.Words.Count
469 
470     Selection.StartOf wdWord
471 
472     While d < wordCount
473 
474         d = d + Selection.MoveRight(wdWord, 1, wdExtend)
475 
476         w = Selection.Text
477         If isPythonLANGKeyword(Trim(w)) = True Then
478 
479             Selection.Font.Color = wdColorViolet
480 
481             Selection.Font.Bold = True
482             
483         ElseIf isOperator(Trim(w)) = True Then
484 
485             Selection.Font.Color = wdColorBrown
486             
487             Selection.Font.Bold = True
488             
489         ElseIf isOthers(Trim(w)) = True Then
490 
491             Selection.Font.Color = wdColorBrown
492 
493             Selection.Font.Bold = True
494 
495         Else
496             
497             Selection.Font.Bold = True
498         End If
499 
500         'move the start of selection to next word
501 
502         Selection.MoveStart wdWord
503 
504     Wend
505 
506     ' prepare For set lIne number
507 
508     Selection.MoveLeft wdWord, wordCount, wdExtend
509 
510     SetLIneNumber
511 
512 End Sub
513 'C syntax hight light
514 Sub CLANGSyntaxHighlight()
515 
516     Dim wordCount As Integer
517 
518     Dim d As Integer
519 
520     ' set the style of selection
521 
522     ' Selection.Style = "ccode"
523 
524     d = 0
525 
526     wordCount = Selection.Words.Count
527 
528     Selection.StartOf wdWord
529 
530     While d < wordCount
531 
532         d = d + Selection.MoveRight(wdWord, 1, wdExtend)
533 
534         w = Selection.Text
535         If isCLANGKeyword(Trim(w)) = True Then
536 
537             Selection.Font.Color = wdColorBlue
538             
539             Selection.Font.Bold = True
540             
541             
542         ElseIf isOperator(Trim(w)) = True Then
543         
544             Selection.Font.Bold = True
545 
546             Selection.Font.Color = wdColorBrown
547             
548             
549         ElseIf isOthers(Trim(w)) = True Then
550 
551             Selection.Font.Color = wdColorBrown
552 
553             Selection.Font.Bold = True
554 
555 
556         ElseIf Trim(w) = "//" Then
557 
558             'lIne comment
559 
560             Selection.MoveEnd wdLine, 1
561 
562             commentWords = Selection.Words.Count
563 
564             d = d + commentWords
565 
566             Selection.Font.Color = wdColorBlack
567 
568             Selection.MoveStart wdWord, commentWords
569 
570          ElseIf Trim(w) = "/*" Then
571 
572             'block comment
573 
574             While Selection.Characters.Last <> "/"
575 
576                 Selection.MoveLeft wdCharacter, 1, wdExtend
577 
578                 Selection.MoveEndUntil ("*")
579 
580                 Selection.MoveRight wdCharacter, 2, wdExtend
581 
582             Wend
583 
584             commentWords = Selection.Words.Count
585 
586             d = d + commentWords
587 
588             Selection.Font.Color = wdColorBlack
589             
590             Selection.MoveStart wdWord, commentWords
591             
592         Else
593             
594             Selection.Font.Bold = True
595         End If
596 
597         'move the start of selection to next word
598 
599         Selection.MoveStart wdWord
600 
601     Wend
602 
603     ' prepare For set lIne number
604 
605     Selection.MoveLeft wdWord, wordCount, wdExtend
606 
607     SetLIneNumber
608 
609 End Sub
610 
611 ' MySQL syntax hight light
612 Sub MySQLForSyntaxHighlight()
613 
614     Dim wordCount As Integer
615 
616     Dim d As Integer
617 
618     ' set the style of selection
619 
620     ' Selection.Style = "ccode"
621 
622     d = 0
623 
624     wordCount = Selection.Words.Count
625 
626     Selection.StartOf wdWord
627 
628     While d < wordCount
629 
630         d = d + Selection.MoveRight(wdWord, 1, wdExtend)
631 
632         w = Selection.Text
633 
634         If isMySQLKeyword(Trim(w)) = True Then
635 
636             Selection.Font.Color = wdColorRed
637 
638             Selection.Font.Bold = True
639                
640         ElseIf isOperator(Trim(w)) = True Then
641         
642             Selection.Font.Bold = True
643 
644             Selection.Font.Color = wdColorBrown
645             
646             
647         ElseIf isOthers(Trim(w)) = True Then
648 
649             Selection.Font.Color = wdColorBrown
650 
651             Selection.Font.Bold = True
652 
653 
654         ElseIf Trim(w) = "#" Then
655 
656             'lIne comment
657 
658             Selection.MoveEnd wdLine, 1
659 
660             commentWords = Selection.Words.Count
661 
662             d = d + commentWords
663 
664             Selection.Font.Color = wdColorBlack
665 
666             Selection.MoveStart wdWord, commentWords
667             
668         Else
669             
670             Selection.Font.Bold = True
671         End If
672 
673         'move the start of selection to next word
674 
675         Selection.MoveStart wdWord
676 
677     Wend
678 
679     ' prepare For set lIne number
680 
681     ' Selection.MoveLeft wdWord, wordCount, wdExtend
682 
683     ' SetLIneNumber
684 
685 End Sub

The following is the source code from the Internet, the original author is not too clear, here is a linkhttps://www.xuebuyuan.com/1843358.html

Thanks again to the author who provided the basic source code.

  1 'script to high light code In document
  2 
  3 Private Function isKeyword(w) As Boolean
  4 
  5     Dim keys As New Collection
  6 
  7     With keys
  8 
  9         .Add "if": .Add "else": .Add "elseif": .Add "case": .Add "switch": .Add "break"
 10 
 11         .Add "for": .Add "continue": .Add "do": .Add "while": .Add "foreach": .Add "echo"
 12 
 13         .Add "define": .Add "array": .Add "NULL": .Add "function": .Add "include": .Add "return"
 14 
 15         .Add "global": .Add "as": .Add "die": .Add "header": .Add "this": .Add "empty"
 16 
 17         .Add "isset": .Add "mysql_fetch_assoc": .Add "class": .Add "style"
 18 
 19         .Add "name": .Add "value": .Add "type": .Add "width": .Add "_POST": .Add "_GET"
 20 
 21     End With
 22 
 23     isKeyword = isSpecial(w, keys)
 24 
 25 End Function
 26 
 27 Private Function isSpecial(ByVal w As String, ByRef col As Collection) As Boolean
 28 
 29     For Each i In col
 30 
 31         If w = i Then
 32 
 33             isSpecial = True
 34 
 35             Exit Function
 36 
 37         End If
 38 
 39     Next
 40 
 41     isspeical = False
 42 
 43 End Function
 44 
 45 Private Function isOperator(w) As Boolean
 46 
 47     Dim ops As New Collection
 48 
 49     With ops
 50 
 51         .Add "+": .Add "-": .Add "*": .Add "/": .Add "&": .Add "^": .Add ";"
 52 
 53         .Add "%": .Add "#": .Add "!": .Add ":": .Add ",": .Add "."
 54 
 55         .Add "||": .Add "&&": .Add "|": .Add "=": .Add "++": .Add "--"
 56 
 57         .Add "'": .Add """"
 58 
 59     End With
 60 
 61     isOperator = isSpecial(w, ops)
 62 
 63 End Function
 64 
 65 Private Function isType(ByVal w As String) As Boolean
 66 
 67     Dim types As New Collection
 68 
 69     With types
 70 
 71         .Add "SELECT": .Add "FROM": .Add "WHERE": .Add "INSERT": .Add "INTO": .Add "VALUES": .Add "ORDER"
 72 
 73         .Add "BY": .Add "LIMIT": .Add "ASC": .Add "DESC": .Add "UPDATE": .Add "DELETE": .Add "COUNT"
 74 
 75         .Add "html": .Add "head": .Add "title": .Add "body": .Add "p": .Add "h1": .Add " h2"
 76 
 77         .Add "h3": .Add "center": .Add "ul": .Add "ol": .Add "li": .Add "a"
 78 
 79         .Add "input": .Add "form": .Add "b"
 80 
 81     End With
 82 
 83     isType = isSpecial(w, types)
 84 
 85 End Function
 86 
 87 Sub SyntaxHighlight()
 88 
 89     Dim wordCount As Integer
 90 
 91     Dim d As Integer
 92 
 93     ' set the style of selection
 94 
 95     Selection.Style = "ccode"
 96 
 97     d = 0
 98 
 99     wordCount = Selection.Words.Count
100 
101     Selection.StartOf wdWord
102 
103     While d < wordCount
104 
105         d = d + Selection.MoveRight(wdWord, 1, wdExtend)
106 
107         w = Selection.Text
108 
109         If isKeyword(Trim(w)) = True Then
110 
111             Selection.Font.Color = wdColorBlue
112 
113         ElseIf isType(Trim(w)) = True Then
114 
115             Selection.Font.Color = wdColorDarkRed
116 
117             Selection.Font.Bold = True
118 
119         ElseIf isOperator(Trim(w)) = True Then
120 
121             Selection.Font.Color = wdColorBrown
122 
123         ElseIf Trim(w) = "//" Then
124 
125             'lIne comment
126 
127             Selection.MoveEnd wdLine, 1
128 
129             commentWords = Selection.Words.Count
130 
131             d = d + commentWords
132 
133             Selection.Font.Color = wdColorGreen
134 
135             Selection.MoveStart wdWord, commentWords
136 
137          ElseIf Trim(w) = "/*" Then
138 
139             'block comment
140 
141             While Selection.Characters.Last <> "/"
142 
143                 Selection.MoveLeft wdCharacter, 1, wdExtend
144 
145                 Selection.MoveEndUntil ("*")
146 
147                 Selection.MoveRight wdCharacter, 2, wdExtend
148 
149             Wend
150 
151             commentWords = Selection.Words.Count
152 
153             d = d + commentWords
154 
155             Selection.Font.Color = wdColorGreen
156 
157             Selection.MoveStart wdWord, commentWords
158 
159         End If
160 
161         'move the start of selection to next word
162 
163         Selection.MoveStart wdWord
164 
165     Wend
166 
167     ' prepare For set lIne number
168 
169     Selection.MoveLeft wdWord, wordCount, wdExtend
170 
171     SetLIneNumber
172 
173 End Sub
174 
175 Private Sub SetLIneNumber()
176 
177     Dim lines As Integer
178 
179     lines = Selection.Paragraphs.Count
180 
181     Selection.StartOf wdParagraph
182 
183     For l = 1 To lines
184 
185         lIneNum = l & " "
186 
187         If l < 10 Then
188 
189             lIneNum = lIneNum & " "
190 
191         End If
192 
193         Selection.Text = lIneNum
194 
195         Selection.Font.Bold = False
196 
197         Selection.Font.Color = wdColorAutomatic
198 
199         p = Selection.MoveDown(wdLine, 1, wdMove)
200 
201         Selection.StartOf wdLine
202 
203     Next
204 
205 End Sub

You can use Notepad++ to accomplish this in three ways. Just so you know, Notepad++ is a more advanced version of Notepad, which supports syntax highlighting of different code files «out of the box» — PHP included!

Download & install it, fire it up, and load up your PHP file. You should automatically see it beautifully coloured (if not, because the file extension is something other than .php, go to Language -> PHP or Language -> P -> PHP).

If you need to change any of the colours, you can easily do so — just go to Settings -> Styler Configurator. From that menu, you can change the various highlighting and font options, to suit your needs — although the default usually suffices for most.

Then, go to Plugins -> NppExport. From there, you have three options you can consider:

  • Export to RTF
  • Export to HTML
  • Copy all formats to clipboard

Start with the last one — «Copy all formats to clipboard» — which will copy the entire file with the highlighted syntax to the clipboard. Once you click it, then open Microsoft Word, and just hit paste! You should see the beautifully syntax-highlighted code. If something goes wrong, then you can try one of the other options (export to RTF/HTML), although I’ve never had a problem with the clipboard method.

Like this post? Please share to your friends:
  • Code format for word
  • Code for word wrap
  • Code for microsoft word
  • Code for date in excel
  • Collection contains vba excel