How do I easily highlight the syntax of PHP code in Word?

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


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.


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.


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 

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.


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