30

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

Ian Ringrose
  • 1,299

5 Answers5

15

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.

sashaeve
  • 261
12

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

soandos
  • 24,600
  • 29
  • 105
  • 136
5

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]

Robert
  • 894
4

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.

Oded
  • 1,146
-1

This is what i did.

End results : enter image description here

https://stackoverflow.com/a/25092977/1161594