How do I add formatted code to a Microsoft Word document?

You may use source highlighting services and copy / paste to Word document. I think it will be the fastest way (upd. simple copypaste from VS will also works).

The second way is to create a style for code block, save it and then apply to all code blocks.


Word does not natively support syntax highlighting, or other code formatting. As such, it is best to create the highlighting etc in a different program (visual studio, eclipse, notepad++, or this website which can do 14 languages and requires no install).

When pasting the text however, doing it directly in the word document itself can lead to headaches like spelling and grammar underlines.

To fix this, instead of directly copying and pasting, use Insert -> Object -> OpenDocument Text. This will open a new document. Paste the text here, and close the window. Your code will now be shown as an object, complete with all of the formatting that showed in the previous window, but with no spelling or grammar check showing.

While it is possible to format code with styles, there are no built in styles to do this, and it is not really what styles are meant to do. If you with to use styles to format your code, just use this style in all of the objects that you create.

Update: If you want to do this with code styles, here are some ideas.

  • Use a paragraph style with a monospaced font
  • Light background (grey works well)
  • Spelling turned off
  • Make sure the line spacing is the way you want it

If you want to add a little more complexity, you can layer character styles on top of the paragraph style to create something similar to the code preview on stackoverflow with different formatting and/or color for class names, instance names, etc.

Setting up the style like this takes work, but you can get a more customizable result than compared to what you can get using the syntax highlighting from notepad++ or Visual Studio.

As a speedup when formatting code with a style, it may help to first do the syntax highlighting in one of those programs, and then use Word's Find utility to search for text that matches a color or other font style so that you do not have to manually identify what is a class name for example in the code manually (it is under more->format in the find prompt).


I like to use Notepad++. In NP++, choose the right syntax, right click on the selected code, choose "plugin commands", click "copy text with syntax highlighting". Paste it in Word and you have very good looking code.

Source: Best way to show code snippets in word? [closed]