Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ms-word

People also ask

How do I paste code into formatting in Word?

Place your cursor in your Word document where you want to paste it. Then go to the Home tab, click the Paste drop-down arrow, and choose Paste Special. Select “HTML Format” and click “OK.” When the code appears in your document, you can format the font or the snippet if you like.

How do I open source code in Word?

View the HTML source code file by selecting the “View” menu, then “HTML Source.” If you have graphics in your document, scroll through the HTML source code to find the reference to the image.


I absolutely hate and despise working for free for Microsoft, given how after all those billions of dollars they STILL do not to have proper guides about stuff like this with screenshots on their damn website.

Anyways, here is a quick guide in Word 2010, using Notepad++ for syntax coloring, and a TextBox which can be captioned:

  1. Choose Insert / Text Box / Simple Text Box
    01word
  2. A default text box is inserted
    02word
  3. Switch to NPP, choose the language for syntax coloring of your code, go to Plugins / NPPExport / Copy RTF to clipboard
    03npp
  4. Switch back to word, and paste into the text box - it may be too small ...
    04word
  5. ... so you may have to change its size
    05word
  6. Having selected the text box, right-click on it, then choose Insert Caption ...
    06word
  7. In the Caption menu, if you don't have one already, click New Label, and set the new label to "Code", click OK ...
    07word
  8. ... then in the Caption dialog, switch the label to Code, and hit OK
    08word
  9. Finally, type your caption in the newly created caption box
    09word

I recently came across this post and found some useful hints. However, I ended up using an entirely different approach which suited my needs. I am sharing the approach and my reasoning of why I chose this approach. The post is longer than I would have liked, but I believe screenshots are always helpful. Hopefully, the answer would be useful to someone.

My requirements were the following:

  1. Add code snippets to a word document, with syntax highlighting for easier visibility and differentiation of code and other text.
  2. Code snippet shall be inline with other text.
  3. Code snippet shall break across pages smoothly without any extra effort.
  4. Code snippet shall have a nice border.
  5. Code snippet shall have spell-check disabled.

My Approach is as listed below:

  1. Use external tool to achieve syntax highlighting requirement 1 above. One could use notepad plus plus as described above. However, I use the tool present here - http://www.planetb.ca/syntax-highlight-word. This gives me the option to use line number, as well as very nice syntax highlighting (Please use Google Chrome for this step, because syntax highlight is not copied when using Mozilla Firefox, as also pointed out by couple of user comments). Steps to achieve syntax highlighting are listed below:
    • Open the website provided above in chrome and Copy the code snippet in the text area. I will be using a sample XML to demonstrate this (XML sample from here - http://www.service-architecture.com/articles/object-oriented-databases/xml_file_for_complex_data.html).
    • Select the language from drop down menu.
    • Click "Show Highlighted" button. It will open a new tab, with syntax-highlighted code snippet, in this case the XML sample we chose. See image below for example. Image shows a sample xml being pasted in the textarea, language selected as XML and the button.
    • To Turn off the line numbers, inspect the page in chrome. Then, under styles, deselect the "margin" property in ".dp-highlighter ol", as shown in the image below. If you want to keep the line numbers, go to next step.
    • Select the syntax-highlighted code and click copy. Now your code is ready to be pasted into Microsoft word. Result of syntax-highlighted XML Thanks to this blog for providing this information - http://idratherbewriting.com/2013/04/04/adding-syntax-highlighting-to-code-examples-online-and-in-microsoft-word/.
  2. To achieve requirements 2, 3 and 4 above, use table in Microsoft word, to insert the code snippet. Steps are listed below:

    • Insert a table with single column.
    • Paste the copied text from step 1. in the table column. I have kept the line numbers to show how well this works with Microsoft word.
    • Apply border, as you like. I have used size 1pt. Resulting Microsoft word snippet will appear as shown in screenshot below. Note how nicely it breaks across the page - NO extra effort needed to manage this, which you would face if inserting "OpenDocument Text" object or if using "Simple TextBox". Syntax-highlighted code snippet pasted in Microsoft Word Table
  3. To achieve requirement 5, follow the steps below:

    • Select the entire table or the text.
    • Go to Review tab. Under Language, choose "Proofing Language". A new pop-up will be presented.
    • Select "Do not check spelling or grammar". Then, click OK. Disable spell-check
    • Resulting text has spell-check disabled. Final result is shown in the image below and meets all the requirements. Final Result - meets all the 5 requirements.

Please provide if you have any feedback or improvements or run into any issues with the approach.


You need to define a style in your Word document and use that for source code. I usually have a style called "Code" which has a monospaced font in a small point size, fixed size tabs, single line spacing, no before/after paragraph spacing, etc. You only need to define this style once and then reuse it. You paste in your source code and apply the "Code" style to it.

Note that some editors (e.g. Xcode on the Mac) add RTF as well as text to the clipboard when copying/pasting between applications - Word recognises RTF and helpfully retains the formatting, syntax colouring, etc.

Source code in Xcode:

enter image description here

Copied and pasted to Word:

enter image description here

(Note: it's a good idea to disable spell-checking in your "Code" style in Word.)


It kind of depends on the IDE. Both Visual Studio and Eclipse, for example, will allow you to copy as RTF and paste into Word, keeping all your formatting.

Notepad++ has a plugin called "NppExport" (comes pre-installed) that allows you to copy to RTF, though I don't care much for Notepad++'s syntax highlighting (it'd definitely be passable though). What it does do is support dozens of languages, whereas the aforementioned IDEs are limited to a handful each (without other plug-ins).


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


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"

enter image description here enter image description here enter image description here


This is related to this answer: https://stackoverflow.com/a/2653406/931265 Creating an object solved all of my problems.

Insert > Object > Opendocument Text

This will open a document window, paste your text, format it how you want, and close it.

The result is a figure. Right click the object, and select 'add a caption'.

You can now make cross references, create a table of figures.