Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
You might find stackoverflow.com/questions/760121/… useful. –  voyager Mar 10 '10 at 17:10
add comment

migrated from stackoverflow.com Feb 16 '10 at 4:44

This question came from our site for professional and enthusiast programmers.

4 Answers

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.

share|improve this answer
1  
how do I use "source highlighting services"? –  Ian Ringrose 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
add comment

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

share|improve this answer
    
@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. –  Ian Ringrose 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
add comment

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]

share|improve this answer
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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