tags:

views:

5971

answers:

10

Does anyone know a good way to display code in Microsoft Word documents?

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.

+6  A: 

Try defining a style called 'code' and make it use a small fixed width font, it should look better then.

frankodwyer
You won't keep syntax highlighting.
Uri
Syntax highlighting is overrated if we're talking a Word document here. But it is also easily implemented in a VB script that colors all text styled as "Code".
Frank Krueger
That's how I do it too with pretty good results, though I also turn off spelling/grammar checking for my "code" style.
Clayton
+4  A: 

Hello

This is a bit indirect, but it works very nicely. Get LiveWriter and install this plugin:

http://lvildosola.blogspot.com/2007/02/code-snippet-plugin-for-windows-live.html

Insert your code using the plugin into a blog post. Select all and copy it to Word.

It looks great and can include line numbers. It also spans pages decently.

HTH

Colby Africa

Colby Africa
A: 

There really isn't a clean way to do it, and it could still look fishy based on your exact style settings.

What you could try to do is to first run a code-to-HTML conversion (there are many programs that do that), and then try to open up the HTML file with word, that might hopefully provide you with the formatted and pretty code, and then copy and paste it into your document.

Uri
+2  A: 

Maybe this is overly simple, but have you tried pasting in your code and setting the font on it to Courier New?

chaos
+9  A: 

I type my code in Visual Studio, and then copy-paste into word. it preserves the colors.

Jimmy
But far from 100% fidelity. The copy process seems to remove a lot of formatting for some reason.
Frank Krueger
This works very well with Eclipse and OpenOffice Writer, by the way (I don't know of Eclipse/Word or VisualStudio/Writer, 'though).
Joachim Sauer
I wonder if it depends whether you use tabs or have Visual Studio convert tabs to spaces. I don't notice any issues with spaces.
DanM
+2  A: 

Use a monospaced font like Lucida Console, which comes with Windows. If you cut/paste from Visual Studio or something that supports syntax highlighting, you can often preserve the colour scheme of the syntax highlighter.

ConcernedOfTunbridgeWells
A: 

The best presentation for code in documents is in a fixed-width font (as it should appear in an IDE), with either a faint, shaded background or a light border to distinguish the block from other text.

Rob
A: 

but how do you disable spell checking just for that styled section

iondiode
+1  A: 

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

  1. go to "insert" tab, click "object" button (it's on the right)
  2. choose "openDocument Text": it will open a new embedded word document
  3. copy past 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.

gargamel
This is a good one!
uosɐſ
+1  A: 

When I've done this, I've made extensive use of styles. It helps a lot.

What I do is create a paragraph style (perhaps called "Code Example" or something like that) which uses a monospaced font, carefully chosen tabs, a very light grey background, a thin black border above and below (that helps visibility a lot) and with spelling turned off. I also make sure that inter-line and inter-paragraph spacing are set right. I then create additional character styles on top (e.g., "Comment", "String", "Keyword", "Function Name Decl", "Variable Name Decl") which I layer on top; those set the color and whether the text is bold/italic. It's then pretty simple to go through and mark up a pasted example as being code and have it come out looking really good, and this is works well for short snippets. Long chunks of code probably should not normally be in something that's going to go on a dead tree. :-)

An advantage of doing it this way is that it is easy to adapt to whatever code you're doing; you don't have to rely on some IDE to figure out whatever is going on for you. (The main problem? Printed pages typically aren't as wide as editor windows so wrapping will suck...)

Donal Fellows