I need to write a document in MS-Word 2007 that contains a lot of examples of VB.NET and C# code.

What is the best way of getting the code to look reasonable in the document?

What styles etc do people use?

(I do not have time to hand edit/format the code, and would be very happy if it just looked the same as it does in developer studio)

In other works what do people do that are writing programming books, there must be a good automated solution for this...

I would like a way to use word styles sheets to control what the code looks like, but don't wish to have to "select" every keyword etc to set it's style.

see also https://stackoverflow.com/questions/387453/how-do-you-display-code-snippets-in-ms-word-preserving-format-and-syntax-highlig/2653406#2653406


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.

  • 1
    how do I use "source highlighting services"? Feb 15 '10 at 9:24
  • 4
    For example, go to source.virtser.net, paste C# code, highlight, copy output and paste to Word.
    – sash
    Feb 15 '10 at 9:40
  • source.virtser.net link give "Page Not found" error, "The page you tried to access does not exist on this server..."
    – JohnC
    May 1 '14 at 19:11

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).

  • @ianRingrose is there some other feature that you are looking for?
    – soandos
    Jan 8 '12 at 16:38
  • I wish the syntax hilighting to be done with word styles so I can change type sizes etc with ease. Jan 10 '12 at 9:44
  • @IanRingrose, updated. Let me know if there is something missing.
    – soandos
    Jan 10 '12 at 9:56
  • i think this is one of the best ways to format code in word. I always had problems with the formating, and as an object i don't have any problems any more. Thanks a lot ; )
    – xtarsy
    Apr 23 '12 at 9:49
  • In MS Word 2007 and up an alternative to "Insert -> Object -> OpenDocument Text" is to select the code after you paste it and use Review | Proofing | Set Language and ensure that "Do not check spelling or grammar" is selected. You might have to apply that setting more than once to get it to stick.
    – JohnC
    May 1 '14 at 19:22

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]


If you setup your VS to look like what you want it to look like in the document, a simple copy-paste of the code will work. It will preserve colors and other formatting.

However, if you ever want to change the style in the document, this will be very difficult to manage. In this case, creating a code style in word and making sure all pasted code is set to it is the way to do (then, if you change the style, everything marked as that style will change). This will not allow you to have syntax highlighting as in VS.

  • There are some concerns, like how will a line longer than the width of the document look like?
    – voyager
    Mar 10 '10 at 17:22

This is what i did.

End results : enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.