Are there any plugins for Microsoft Word to format source code samples inside a regular document?

13

2

I need to write a technical document in Microsoft Word, with source code samples sprinkled throughout the text.

Is there any way, without leaving Microsoft Word (maybe a plugin?), to format the source code snippets within the document in the same easy way that SuperUser.com provides - a little button that defines everything as code and formats it appropriately?

lipton

Posted 2009-09-19T15:49:14.327

Reputation: 789

Are you looking for something that will color keywords or just something like the code tag in HTML? – Joshua Nurczyk – 2009-09-19T17:12:03.413

Answers

5

You can create a "code style" using the Format / Styles and Formatting option. From the "Styles and Formatting" sidebar, create a new style to use for code. You can specify the font, size, tabs, borders, shading, shortcut keys, etc...

Once you have a new code style, select your code in the document and apply your new style. You can select multiple locations in text by holding the button while selecting the text.

Another advantage of using styles is that you can easily change the format of any text that uses that style. You just change the properties of the style, and any text that uses that style is automatically updated.

Chrisbux

Posted 2009-09-19T15:49:14.327

Reputation: 548

11

I know you asked for a solution without leaving Word. But still I hope you realize that if you copy code from within Visual Studio and past it into Word, it will retain all formating (including the color scheme).

When copy-pasting from other locations, at the very minimum you will retain space and newlines information which will serve as the basis for code formating when you then apply the advise on this thread by Chrisbux, you can change the font style to a fixed width like courier, terminal, etc.

Word will not recognize differences between identifiers, keywords, operators, literals, etc. And neither there is a plugin to my knowledge that can do it. Most code samples on Word documents I've seen (and I've seen many as a software developer working for a company that documents the development process every step of the way), are simply done by using a style to place the code into a well delineated box and define a fixed-width font.

A Dwarf

Posted 2009-09-19T15:49:14.327

Reputation: 17 756

+1 for using Visual Studio; that's how I do it as well – fretje – 2009-09-19T17:39:20.720

Downvoting this because pasting from VS to Word gave me results that I don't like. Lines are double spaced, and I want a monospaced font. – Elroy Flynn – 2012-05-27T00:00:07.433

4

Not technically an answer to the question you asked, but there is a plugin to do exactly what you're looking to do... for OpenOffice.

http://extensions.services.openoffice.org/project/CodeFormatter

It's still under development, and isn't perfect nor lightning-fast, but for the languages it supports (C[++], Java, XML, Perl, Python, and Basic) it does a pretty damn sweet job! And unlike Visual Studio copy-n-pasting, you can adjust the formatting just by modifying the styles it uses (and generates) to tag up the colorized code.

Seeing as OpenOffice is a $0 product, and it seems to do what you're looking for (unlike Word), it's at least worth considering, no?

FeRD

Posted 2009-09-19T15:49:14.327

Reputation: 989