What is the best way to insert source code examples into a Microsoft Word document?

前端 未结 15 1491
南笙
南笙 2020-12-22 16:05

I have to write some documents that will include source code examples. Some of the examples will be written from the IDE, and others would be written in place. My examples a

相关标签:
15条回答
  • 2020-12-22 16:20

    These answers look outdated and quite tedious compared to the web add-in solution; which is available for products since Office 2013.

    I'm using Easy Code Formatter, which allows you to codify the text in-place. It also gives you line-numbering options, highlighting, different styles and the styles are open sourced here: https://github.com/armhil/easy-code-formatter-styles so you could extend the styling yourself. To install - open Microsoft Word, go to Insert Tab / click "Get Add-ins" and search for "Easy Code Formatter"

    0 讨论(0)
  • 2020-12-22 16:21

    I have tried all your methods, but they didn't work for me, in fact I have created an easier method using MS Word Tables.

    Pros:

    1. More beautiful
    2. Easier to manage & more consistent
    3. Are less prone to problems
    4. No need for external plugins or MS Word micro coding.
    5. Easier to handle by simple users (such as myself).

    Cons: It will not maintain code colouring although someone could improve my trick.

    Steps:

    1. Insert a 3x3 table, in my case I always make the total width of the table equals the free page's width (3 rows minimum to test the tables style).

    2. Use invisible borders ("No Borders" option), and activate "View Gridlines" option. it should have this aspect. Be ware that those lines are for you to see the table's grid, and the will not be printed.

    1. Make the adjustments to cells' spacing and columns' width to get the aspect you like. (You will have to get in "Table Properties" for fine tuning).

    2. Create a "Paragraph Style" with the name of "Code" just for your code snippets (check https://stackoverflow.com/a/25092977/8533804 to get the idea, you don't have to follow all of it)

    3. Create another "Paragraph Style" with the name of "Code_numberline" that will be based upon the previous created style.

    4. In the newly created "Code_numberline" add the numbering style that you like (this will automate line numbering).

    5. Apply "Code_numberline" to the first column, and "Code" to the 3 column.

    6. Add a fill in the middle column.

    7. Save that table style and enjoy!

    0 讨论(0)
  • 2020-12-22 16:23

    In Word, it is possible to paste code that uses color to differentiate comments from code using "Paste Keep Source Formatting." However, if you use the pasted code to create a new style, Word automatically strips the color coded text and changes them to be black (or whatever the auto default color is). Since applying a style is the best way to ensure compliance with document format requirements, Word is not very useful for documenting software programs. Unfortunately, I don't recall Open Office being any better. The best work-around is to use the default simple text box.

    0 讨论(0)
  • 2020-12-22 16:24

    This is what i did.

    End results : enter image description here

    https://stackoverflow.com/a/25092977/1161594

    0 讨论(0)
  • 2020-12-22 16:25

    Use this - http://hilite.me/

    hilite.me converts your code snippets into pretty-printed HTML format, easily embeddable into blog posts, emails and websites.

    How: Just copy the source code to the left pane, select the language and the color scheme, and click "Highlight!". The HTML from the right pane can now be pasted to your blog or email, no external CSS or Javascript files are required.

    For Microsoft Word document: Copy the the content from the Preview section and paste to your Microsoft Word document.

    3 sections : Source Code , HTML and Preview

    0 讨论(0)
  • 2020-12-22 16:25

    On a Mac I find this solution with vim to be wonderful:

    https://github.com/zerowidth/vim-copy-as-rtf

    0 讨论(0)
提交回复
热议问题