33

How do I easily write and syntax-highlighted PHP code (or any others programming language) in a Word 2009 document?

Christian Studer
  • 464
  • 1
  • 5
  • 11

10 Answers10

53

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.

Breakthrough
  • 34,227
  • 10
  • 105
  • 149
  • 2
    Thanks, I already had Notepad++ installed, but didn't knew about the NppExport-plugin. I'll go with your solution. – Christian Studer 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! – Josh Hunt Sep 11 '09 at 12:23
  • Absolute perfection! – dcousineau Oct 04 '09 at 22:20
  • 3
    Nice. One additional tip (half related): You can then highlight the pasted text, then on the Review tab of word, select Language, Set Proofing Language. Tick 'Do not check spelling or grammar'. Now you won't get the red underlines JUST for the pasted text. – Ray Mar 23 '15 at 02:15
  • Notice that if you have the option to create links enabled (Notepad++ now created a clickable link when something starts with http:// or similar), NppExport will throw an `Unknown Exception`. – Ismael Miguel Jul 23 '15 at 14:49
  • In Notepad++ v7.7.1, NppExport maintains font name, size, and bold, but not font color. Works fine in 7.6.x. See: [NppExport plugin - 'copy HTML to clipboard' does not work anymore](https://notepad-plus-plus.org/community/topic/17716/nppexport-plugin-copy-html-to-clipboard-does-not-work-anymore) – Eliahu Aaron Jul 11 '19 at 15:03
10

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.

Snark
  • 32,299
  • 7
  • 87
  • 97
3

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 
Greg B
  • 1,048
  • 6
  • 16
  • 25
3

I found this question looking for the same thing for Outlook on Mac OS, but Notepad++ doesn't exist for OSX and I use Sublime Text (which has the advantage of being cross-platform).

For this there's a plugin called SublimeHighlight.

Assuming you use Sublime Package Control, the steps to install SublimeHighlight are:

  1. Press cmd shift P
  2. Run "Package Control: Add channel"
  3. Paste the github url: https://github.com/n1k0/SublimeHighlight
  4. Press cmd shift P again, and run "Package Control: Install Package"
  5. Install the "Highlight" Package, and restart Sublime Text.

Then to copy text you can press cmd shift P, and run "SublimeHighlight: copy to clipboard as HTML" (or RTF). These commands can also be assigned keyboard shortcuts.

Note that on Windows or Linux the shortcut key for the Command Palette is different, but it can be accessed by going to Tools > Command Palette.

Alex Forbes
  • 1,074
  • 10
  • 9
2

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

Jamie
  • 21
  • 1
0

You can using Plugin Syntax Highlight in Ms.Word https://store.office.com/syntax-highlighter-WA104315019.aspx?assetid=WA104315019 . i follow that step and work

Output

  • Welcome to Super User. Please realize you have posted an answer to a question asked more than 5 years ago, and which already has an accepted answer. Therefore, you may not receive acknowledgement. – CharlieRB Feb 09 '16 at 19:49
0

I'm using Easy Code Formatter as highlighted here: https://stackoverflow.com/questions/387453/how-do-you-display-code-snippets-in-ms-word-preserving-format-and-syntax-highlig/55907121#55907121

It also allows you to select line-numbering, highlighting and gives you different theme options which are open-sourced.

code formatter

0

Copy-paste from Code Writer (free software)

enter image description here

https://apps.microsoft.com/store/detail/code-writer/9WZDNCRFHZDT?hl=en-us&gl=US

Rasmus Larsen
  • 396
  • 1
  • 8
  • 16
0

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.

Josh Hunt
  • 21,185
  • 20
  • 84
  • 123
-1

You may want to consider abandoning Word altogether and use AsciiDoc instead. AsciiDoc is both a simple markup language and a processor to render documents into other formats. It supports HTML and DocBook. DocBook format can be converted to other formats (e.g. PDF, ePub) using additional tools (e.g. a2x).

By default, it uses GNU source-highlight to color code source code.

Craig
  • 899
  • 3
  • 16
  • 32