Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 248
Lawrence Barsanti Avatar asked Dec 22 '08 21:12

Lawrence Barsanti


People also ask

How do I highlight code in Word?

Open the target document in Microsoft Word and place the cursor where the source code will appear. Select Insert. In the Text group, select Object. In the Object dialog box, select the Create New tab.

Which Word format preserves formatting and allows editing?

Portable Document Format (PDF), a PostScript-based electronic file format that was developed by Adobe Systems. It preserves document formatting and enables file sharing. Files that use the PDF file format can be saved and opened by using Word 2019, Word 2016, and Word 2013.


2 Answers

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.

like image 185
gargamel Avatar answered Sep 29 '22 00:09

gargamel


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++

like image 21
Saad Avatar answered Sep 28 '22 23:09

Saad