How do you display code snippets in MS Word preserving format and syntax highlighting?
Here is the best way, for me, to add code inside word:
- go to
Insert
tab,Text
section, clickobject
button (it's on the right) - choose
OpenDocument Text
which 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.
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!
Update 29/06/2013:
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 !!!
Bit Rejoice!
Code Format is available here: https://appsource.microsoft.com/en-us/product/office/WA104379501?tab=Overview