Categories
code-formatting ms-word

How do you display code snippets in MS Word preserving format and syntax highlighting?

2494

Does anyone know a way to display code in Microsoft Word documents that preserves coloring and formatting? Preferably, the method would also be unobtrusive and easy to update.

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.

7

  • I have gone with a mixed solution. Copy with RTF formatting, pasting into OpenOffice Writer, removing the background (if there is any) and then pasting the code into MS Word. For some reason I can’t remove the background in Word 🙁 Anyways, the “Copy on steroids” plugin to do this in PhpStorm, WebStorm, PyCharm is awsome!

    Aug 22, 2014 at 22:46

  • 6

    Starting with Office 365, I don’t know maybe even for Word 2013 or 2016. It has Online Addin called “Code Format” . Install that Office Addin and in your word doc you can just select all code (even multiple pages) and click convert it button from the Code Format addin and it converts it into a formatted color code with line numbers. I could not find the option to format without line numbers though !!! But nevertheless, not too bad !!

    Nov 7, 2016 at 19:04

  • Outside of any specific add-in that would dedicate to format code the best solution is to use styles or alternatively images. Styles are meant for formatting. I used to use many forks from Star Office to Libre Office today but I just got into Word and I found how to do it just by exploring the style config. Just use Notepad++ to export RTF then use styles. Some of the things I did is add a border, a background color I like, check “Do not add spacing between the same style” or alike (I have the French GUI so I don’t know the exact english caption) and anything you like.

    – 猫IT

    Jan 7, 2019 at 23:01

  • Code Format is unavailable – but Easy Code Formatter here works pretty well: appsource.microsoft.com/en-us/product/office/…

    Apr 29, 2019 at 16:11

  • Just be careful with online code highlighting tool, once you submit your code, you never know what ll happen to your code. The tool provider may claimed ur code or anything. Just a warning though.

    – Ram

    Mar 18, 2021 at 11:04

2508

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

  1. Go to Insert tab, Text section, click Object button (it’s on the right)
  2. Choose OpenDocument Text which will open a new embedded word document
  3. Copy and paste your code from Visual Studio / Eclipse inside this embedded word page
  4. Save and close

Advantages

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.

35

  • 27

    Brilliant! I’ve been fiddling with styles, tables and other workarounds for years. This fixes everything in one go. Note that you also get the bonus of adding a caption to your code. I added a new label type “Listing” for brownie points 🙂

    – Nebula

    Jan 31, 2013 at 12:47

  • 8

    Please note tath this requires open/libre office to be installed on your machine.

    – BetaRide

    Oct 10, 2013 at 6:06

  • 16

    @BetaRide No, it doesn’t. I just used this on a document on a machine without OpenOffice installed. OpenDocument refers to a file format with support from MS Office among others: en.wikipedia.org/wiki/OpenDocument

    – Jamie F

    Oct 10, 2013 at 17:51


  • 57

    Can’t see that option on Word 2011 on OSX 🙁

    Nov 8, 2013 at 14:10

  • 52

    For Word 2011 on OSX use: Insert -> Object…Microsoft Word Document

    – s_t_e_v_e

    Apr 7, 2014 at 17:19

1160

Download and install Notepad++ and do the following:

  1. Paste your code in the window;

  2. Select the programming language from the language menu;

  3. Select the text to copy;

  4. Right click and select Plugin commands -> Copy Text with Syntax Highlighting;

  5. Paste it into MS Word and you are good to go!

Update 29/06/2013:

Notepad++ has a plugin called “NppExport” (comes pre-installed) that allows you to copy to RTF, HTML and ALL. It permits dozens of languages, whereas the aforementioned IDEs are limited to a handful each (without other plug-ins).

I use Copy all formats to clipboard and “paste as HTML” in MS word.

screenshot from notepad++

25

  • 9

    This worked beautifully. I already had Notepad++ for other code projects, but never thought of using it. Combined with a Word style that added a faint blue background, border, and disabled spell check, it looks pretty good, and is fairly fast for a number of files, as opposed to methods involving file exports and imports. Although I wish there was a way to add it as a field that would automatically check the original .java files for updates, and keep the latest source code in the Word file.

    – Neil

    Feb 4, 2011 at 18:31

  • 95

    Thanks for this hint. Plugins -> NppExport -> Copy RTF to Clipboard also works.

    May 16, 2011 at 17:32

  • 35

    This worked very well. In Word 2007+, to add a border and disable spell check: 1) select your code; 2) ‘Home->Styles->Save selection as new Quick Style’ (it is a drop button); 3) Name it ‘Code’ or similar; 4) Modify; 5) Format->Border (lower left) to add border; 6) Format->Language [X] Do not check spelling or grammar. Now, you can apply the same formatting ‘Code’ to text pasted from NppExport.

    – Blazes

    Jul 31, 2011 at 9:52

  • 2

    This one works great, couldn’t get the Word Document inside the Word Document to play nice. Folks this is the way to go!

    – Urda

    Nov 20, 2011 at 23:25

  • 6

    @BennyNeugebauer : Line Number can be added by configure the Format->Numbering options, Define new number formate, remove the dot in first style. You can get to the Format option by either following @ Blazes ‘s steps OR in style select the new style->manage styles->Modify

    – Mzq

    Feb 13, 2014 at 7:20

199

enter image description here

Code Format Addin is now available in Office Addins with Office 365 !
Just select the code and click convert it and it converts the code text to formatted color code with line numbers !!!

Bit Rejoice!

Code Format is available here: https://appsource.microsoft.com/en-us/product/office/WA104379501?tab=Overview

14

  • 17

    only post i saw where the answer is an add on in word. easiest solution, this should be the accepted answer in 2017!

    – cre8

    Jul 12, 2017 at 8:31

  • 2

    @schauhan It was never meant to remove spelling errors / warnings. The purpose of this plug-in is to format the code with keyword highlights and indents.

    Nov 15, 2017 at 0:28

  • 48

    No longer available

    Nov 15, 2018 at 21:05

  • 16

    Found the Easy Code Formatter at the addon list, worked pretty well.

    Mar 29, 2019 at 19:02

  • 2

    Alternatively, a word add-in called Easy Syntax Highlighter provides language-specific highlighting with multiple themes per language and automatic language detection.

    – JDG

    Apr 9, 2020 at 9:54