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.
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.
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:
- Code and main document remain separate from each other (programmers love this approach, don’t they?)
- 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.
- And obviously, it’s easier, quicker method. Code remains in it’s original format and syntax highlights.
- 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.
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:
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 →
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.
In Programming
|
Keywords
|
Thanks to…
Like
Tweet
Share
Share
Share
Share
Share
Save
Paste the code and select the style (colors) desired
Unhighlighted code
Options
-
Language
Autodetect Select one
-
Select language
-
Select style
Highlighted code
Highlighted code (HTML)
Highlight!
Loading…