How do you display code snippets in MS Word preserving format and syntax highlighting?
Does anyone know a way to display code in Microsoft Word documents that preserves coloring and formatting? Preferably, the method would also be unobtrusive and easy to update.
I have tried to include code as regular text which looks awful and gets in the way when editing regular text. I have also tried inserting objects, a WordPad document and Text Box, into the document then putting the code inside those objects. The code looks much better and is easier to avoid while editing the rest of the text. However, these objects can only span one page which makes editing a nightmare when several pages of code need to be added.
Lastly, I know that there are much better editors/formats that have no problem handling this but I am stuck working with MS word.
Here is the best way, for me, to add code inside word:
- go to
objectbutton (it's on the right)
OpenDocument Textwhich will open a new embedded word document
- copy and paste your code from Visual Studio / Eclipse inside this embedded word page
- save and close
The result looks very nice. Here are the advantages of this method:
- the code keeps its original layout and colors
- the code is separated from the rest of the document, as if it was a picture or a chart
- spelling errors won't be highlighted in the code (this is cool !)
And it takes only few seconds.
Read more... Read less...
Download and install Notepad++ and do the following:
Paste your code in the window;
Select the programming language from the language menu;
Select the text to copy;
Right click and select Plugin commands -> Copy Text with Syntax Highlighting;
Paste it into MS Word and you are good to go!
Notepad++ has a plugin called "NppExport" (comes pre-installed) that allows you to copy to RTF, HTML and ALL. It permits dozens of languages, whereas the aforementioned IDEs are limited to a handful each (without other plug-ins).
I use Copy all formats to clipboard and "paste as HTML" in MS word.
Code Format Addin is now available in Office Addins with Office 365 ! Just select the code and click convert it and it converts the code text to formatted color code with line numbers !!!
Code Format is available here: https://appsource.microsoft.com/en-us/product/office/WA104379501?tab=Overview
After reading a lot of related answers, I came across my own solution, which for me is the most suitable one.
As you can see, it is the same syntax highlighting like on Stack Overflow which is quite awesome.
Steps to reproduce:
on Stack Overflow
- Goto Ask Question (preferably with Chrome)
- Paste Code and add a language tag (e.g. Java) to get syntax hightlighting
- Copy code from preview
- Insert > Table > 1x1
- Paste code
- Table Design > Borders > No Border
- Select code > Edit > Find > Replace
Search Document ^p (Paragraph Mark) Replace With ^l (Manual Line Break)
(This is required to remove the gaps between some lines)
- Select code again > Review > Language > check "Do not check spelling or grammar"
- Finally add a caption using References > Insert Caption > New Label > name it "Listing" or sth
Sample code thanks to this guy
There is a nice Online Tool for that : http://www.planetb.ca/syntax-highlight-word
Just copy the generated code and paste it in your word editing software. So far I've tried it on MS Word and WPS Writer, works really well. Doesn't play nice with Firefox but works just fine on Chrome (and IE too, but who wants to use that).
One of the main benefits is that, unlike the Code Format Add-In for Word, it does NOT mess with your code, and respects various languages syntax. I tried many other options offered in other answer but I found this one to be the most efficient (quick and really effective).
There is also another onlinz tool quoted in another answer (markup.su) but I find the planetB output more elegant (although less versatile).
I type my code in Visual Studio, and then copy-paste into word. it preserves the colors.