How do you display code snippets in MS Word preserving format and syntax highlighting?

Lawrence Barsanti picture Lawrence Barsanti · Dec 22, 2008 · Viewed 1.4M times · Source

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.

Answer

gargamel picture gargamel · Apr 16, 2010

Here is the best way, for me, to add code inside word:

  1. go to Insert tab, Text section, click object button (it's on the right)
  2. choose OpenDocument Text which will open a new embedded word document
  3. copy and paste your code from Visual Studio / Eclipse inside this embedded word page
  4. 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.