Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

ms-word - 如何以MS Word保留格式和语法高亮显示代码片段?(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?

(有谁知道一种在Microsoft Word文档中显示可保留颜色和格式的代码的方法?)

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。)

  ask by Lawrence Barsanti translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(对我来说,这是在word中添加代码的最佳方法:)

  1. go to Insert tab, Text section, click object button (it's on the right)

    (转到“ Insert选项卡的“ Text部分,单击“ object按钮(在右侧))

  2. choose OpenDocument Text which will open a new embedded word document

    (选择“ OpenDocument Text ,这将打开一个新的嵌入式Word文档)

  3. copy and paste your code from Visual Studio / Eclipse inside this embedded word page

    (从Visual Studio / Eclipse复制并粘贴您的代码到此嵌入式word页面中)

  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.

(而且只需要几秒钟。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...