How to Insert Source Code Into a Word Document

It can be done without taking snapshots of each segment

What to Know

  • To embed a second document into a Word document: Insert > Object > Create New > Word Document > clear Display as icon > OK.
  • You can also use Paste Special to insert a variety of data into a document, including code.

This article explains issues with using source code in Word for Microsoft 365, Word 2019, Word 2016, Word 2013, Word 2010, and Word 2007 and instructions for embedding a second document in a Word file. It also provides information on pasting other data into a Word document.

The Problem With Using Source Code in Word

Programmers write software programs using languages such as Java, C++, and HTML. Programming languages 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.

Given how Microsoft Word structures documents, inserting and working with source code is more difficult than working in a dedicated code editor. However, document embeds create a container that protects source code from being reformatted.

One way to avoid these formatting issues is to paste the source code into a separate document within the 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. Select Insert.

    A Word document with the Insert field highlighted
  3. In the Text group, select Object.

    A Word document with the Object option highlighted in the Insert menu
  4. In the Object dialog box, select the Create New tab.

    Object dialogue box with the Create New tab highlighted in Word
  5. In the Object type list, Select Microsoft Word Document.

    Create New Object box in Word with the Microsoft Word Document option highlighted

    In Word 2007, select OpenDocument Text.

  6. Clear the Display as icon check box.

    Create New Object window in Word with the Display as icon box highlighted
  7. Select OK.

    Create New Object window in Word with the OK button highlighted
  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 the spaces, tabs, and other formatting problems. Spelling errors and grammatical errors are highlighted in the document, but these errors are ignored when the code is inserted in the original document.

    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.

Using Different Paste Types in Word

The most up-to-date versions of Word handle code better than they used to. Word for Microsoft 365 supports several forms of pasting, including with and without source formatting. So pasting a block of code from, for example, Microsoft Visual Studio Code will look different based on the paste type. If you select Paste Special, each of the three options yields a different result:

  • Unformatted Text: All code is pasted as unformatted, so you'll lose indenting, color, typeface, and related contextual clues.
  • HTML Format: From VSC, a paste-as-HTML renders what appears to be a photo of the code, complete with the text editor's background color. This code block is editable, and you can remove the background color in the Paragraph Fill menu option.
  • Unformatted Unicode Text: Pastes the text as is but strips the text and background colors. Re-format the code as needed.
FAQ
  • How do I see formatting marks and codes in Word?

    To see formatting marks and codes in Word temporarily, go to Home and select the Show/Hide icon to toggle marks on and off. To keep them on permanently, go to File > Options > Display > Show all formatting marks > OK.

  • How do I add links in Word documents?

    To add a link in a Word document, highlight the text you want to hyperlink, right-click it, and select Link. Alternatively, select Insert > Links > Link and enter the URL.

  • How do I convert a Word document to HTML?

    To convert a Word document to HTML, go to File > Save As. Under Save as Type, choose .html. You can also use an editor like Dreamweaver.

Was this page helpful?