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

发布时间 2023-06-06 13:42:51作者: ChuckLu

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

回答1

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.

 

For Word 2011 on OSX use: Insert -> Object...Microsoft Word Document Apr 7, 2014 at 17:19

 

 

回答2

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.

 

Thanks for this hint. Plugins -> NppExport -> Copy RTF to Clipboard also works. May 16, 2011 at 17:32