How do I easily write and syntax hightlight PHP (or any other language really) code in a Word 2009 document?

link|improve this question
3  
why would you want to? – RCIX Sep 11 '09 at 10:02
I would just use Notepad++. It's great and has syntax highlighting for hundreds of languages – D'Arvit Sep 11 '09 at 10:32
1  
@RCIX: I'm writing a concept document which will feature only code snippets, but no actual code. – christian Sep 11 '09 at 11:51
feedback

5 Answers

up vote 23 down vote accepted

You can use Notepad++ to accomplish this in three ways. Just so you know, Notepad++ is a more advanced version of Notepad, which supports syntax highlighting of different code files "out of the box" - PHP included!

Download & install it, fire it up, and load up your PHP file. You should automatically see it beautifully coloured (if not, because the file extension is something other than .php, go to Language -> PHP or Language -> P -> PHP).

If you need to change any of the colours, you can easily do so - just go to Settings -> Styler Configurator. From that menu, you can change the various highlighting and font options, to suit your needs - although the default usually suffices for most.

Then, go to Plugins -> NppExport. From there, you have three options you can consider:

  • Export to RTF
  • Export to HTML
  • Copy all formats to clipboard

Start with the last one - "Copy all formats to clipboard" - which will copy the entire file with the highlighted syntax to the clipboard. Once you click it, then open Microsoft Word, and just hit paste! You should see the beautifully syntax-highlighted code. If something goes wrong, then you can try one of the other options (export to RTF/HTML), although I've never had a problem with the clipboard method.

link|improve this answer
1  
Thanks, I already had Notepad++ installed, but didn't knew about the NppExport-plugin. I'll go with your solution. – christian Sep 11 '09 at 11:52
Yeah, it's quite useful for copying & pasting to Word to print documents out... I love it. :) – Breakthrough Sep 11 '09 at 11:53
This is why I love SU. I had no idea you could do this! – David Pearce Sep 11 '09 at 12:23
Absolute perfection! – dcousineau Oct 4 '09 at 22:20
+1 Excellent answer, thank you – Andreas Grech Oct 26 '09 at 11:15
feedback

Send your code to an online syntax highlighting website, this one for example (select the langage yourself, Autodetect doesn't work very well). There is a list of other online syntax highlighting site here.

Copy the highlighted code and paste it into Word.

link|improve this answer
Note: you have to use IE for this. Opera doesn't work, but I don't know about FF/Safari/Chrome. – Vilx- Sep 11 '09 at 10:24
1  
Works fine with Chrome and Firefox. – Snark Sep 11 '09 at 10:56
Thank you, it works great with Firefox indeed. – christian Sep 11 '09 at 11:51
feedback

You could install the .PHPS file type into Apache then copy the output from your browser into Word.

in your http.conf

AddType application/x-httpd-php-source .phps
link|improve this answer
Creative, but not exactly easy (Requires a webserver...). ;-) – christian Sep 11 '09 at 11:53
Fair point :) – Greg B Sep 11 '09 at 14:21
feedback

Without manually creating character styles for each element of the language and applying them manually, you can't.

Your best bet is to take a screenshot in a editor that does do syntax highlighting and insert that into your document, or copy-and-paste the already colourful code from another program (perhaps a paste bin) and paste it into Word and hope that the colour formatting gets preserved.

link|improve this answer
Some browsers carry over formatting and colour when you copy & paste. ;) – Breakthrough Sep 11 '09 at 10:56
feedback

Syntax highlighting many languages in Word documents: http://www.planetb.ca/2008/11/syntax-highlight-code-in-word-documents/

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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