How to Insert Source Code Into a Word Document

Program code
traffic_analyzer on Getty Images

If you are a programmer or software developer, then you know the futility of trying to use Microsoft Office Word for source code work. While you cannot use MS Word to write or implement source code, you can insert it into a Word document to prepare source code for printing or sharing without taking snapshots of each segment of code.

The Problem With Using Source Code in Word

Programmers write software programs using programming languages that including Java, C++, and HTML. The programming language provides a series of instructions. All the instructions a programmer uses to build a program are known collectively as the source code. Source code typically works behind the scenes and remains unseen by most internet users. However, programmers and others may occasionally want to print out or display source code as part of a presentation.

If you paste source code in a Word program, you experience errors that include reformatting of text, indentations, link creation, and a ridiculous amount of spelling errors.

Avoid these problems by pasting the source code into a separate document window you open within your main document.

Insert a Second Document Into a Word Document

Here's how to paste source code into a Word document using a second embedded document.

  1. Open a document in Microsoft Word.
  2. Click the Insert tab on the ribbon.
  3. Click Object at the right side of the ribbon.
  4. Click Microsoft Word Document ensuring Display as icon is unchecked. (In Word 2007, click OpenDocument Text ensuring Display as icon is unchecked.)
  5. Click OK to open a new document window entitled Document in [your file’s name]. Save the document.
  6. Copy and paste the source code into the new document. Word automatically ignores all the spaces, tabs, and other formatting issues. You see spelling errors and grammatical errors highlighted in the document, but when it is inserted in the original document, they are ignored.
  7. Save the source code document. Close the document. The source code appears at the top of the main document.
  8. Click once on the source code to select it. Copy and paste it in the place you want it to appear in the final document.
  9. Resume work on the main document.

Note: This works for only a single page of code.