How do you display code snippets in MS Word preser

2020-01-23 10:29发布

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.

标签: ms-word
30条回答
放荡不羁爱自由
2楼-- · 2020-01-23 10:35

If you are using Sublime Text, you can copy the code from Sublime to MS Word preserving the syntax highlighting.

Install the package called SublimeHighlight.

In Sublime, using your cursor, select the code you want to copy, right click it, select 'copy as rtf', and paste into MS Word.

查看更多
Anthone
3楼-- · 2020-01-23 10:36

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

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.

查看更多
做自己的国王
4楼-- · 2020-01-23 10:36

I'm using Word 2010 and I like copying and paste from a github gist. Just remember to keep source formatting!

I then change the font to DejaVu Sans Mono.

You can opt to copy with or without the numbering.

查看更多
贪生不怕死
5楼-- · 2020-01-23 10:40

The best presentation for code in documents is in a fixed-width font (as it should appear in an IDE), with either a faint, shaded background or a light border to distinguish the block from other text.

查看更多
Emotional °昔
6楼-- · 2020-01-23 10:41

If its Java source code copy it to Visual Studio and then copy it back to Word.

查看更多
家丑人穷心不美
7楼-- · 2020-01-23 10:46

Maybe this is overly simple, but have you tried pasting in your code and setting the font on it to Courier New?

查看更多
登录 后发表回答