How to Insert Source Code Into a Word Document

It can be done without taking snapshots of each segment

Program code
traffic_analyzer on Getty Images

If you're a programmer or software developer, you may find it a challenge to use Microsoft Word for source code work. There is a way to insert source code into a Word document, without taking snapshots of each segment of code.

Instructions in this article apply to Word 2019, 2016, 2013, 2010, 2007; and Word for Office 365.

The Problem With Using Source Code in Word

Programmers write software programs using languages like Java, C++, and HTML. They use different formatting and symbols from regular languages, so pasting a snippet of code into Word from a programming application causes errors such as text reformatting, indentation shifts, link creation, and misspellings.

One way to avoid these formatting issues is to paste the source code into a separate document that displays within your main Word document.

Embed a Second Document Into a Word Document

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

These instructions only work with a single page of code.

  1. Open the target document in Microsoft Word and place the cursor where the source code will appear.

  2. Go to Insert.

  3. In the Text group, select Object.

  4. In the Object dialog box, select the Create New tab.

    Microsoft Word Object dialog box with Microsoft Word Document selected
  5. In the Object type list, choose Microsoft Word Document. In Word 2007, choose OpenDocument Text.

  6. Clear the Display as icon check box.

  7. Select OK.

  8. A new document opens, titled Document in [target document file name]. Save the document in the same folder as the target document.

  9. Copy and paste the source code into the new document. Word automatically ignores all the spaces, tabs, and other formatting issues. Spelling errors and grammatical errors are highlighted in the document, but when the code is inserted in the original document, these errors are ignored.

    Microsoft Word displaying source code to insert into a Word presentation document
  10. Save and close the source code document. The source code appears in the main document.

    Microsoft Word document with source code inserted
  11. Resume work on the main document.