How to Insert Source Code Into a Word Document

Program code
traffic_analyzer on Getty Images

While most people have no need for, or even knowledge of source code, there are a few people who may find this useful. If you are a programmer or software developer, then you will know the struggle of trying to use Microsoft Office Word for source code work. While you cannot use MS Word to write or implement source code, inserting it into a document is a great way to prepare source code for printing or sharing in presentations without taking snapshots of each segment of code.

Note: Please note that while I am only providing explicit instructions for doing this with MS Word, you can also use this same process to insert source code into all other Office programs.

First Things First

While I understand that by reading past the first paragraph of this article, you know what source code is, I will provide a basic description for anyone who has decided to be adventurous or was just curious about the process.

Programmers write software programs using a programming language (Java, C++, HTML, etc.). The programming language provides a series of instructions they can use to create the program they want. All the instructions a programmer uses to build the program are known as source code.

If you ever decide to place source code into an Office program (2007 or newer), you will experience quite a few common errors including, but not limited to:

  1. Reformatting of text
  2. Indentations
  3. Link creation
  1. And lastly, ridiculous amounts of spelling errors.

Regardless of all these errors that occur as a result of traditional copy and paste, by following this tutorial, you can easily and accurately reference or share source code content from other sources.

Let’s Get Started

Before you start, you will obviously need to open up a new or existing MS Word document.

After you have opened the document, place the typing cursor wherever you wish to insert the source code. Next, you will need to select the “Insert” tab on the ribbon at the top of the screen.

Once you are on the “Insert” tab, click on the “Object” button on the right side. Alternately, you can simply press “Alt + N” then “J.” Once the “Object” dialog box opens, you will need to select “OpenDocument Text” near the bottom of the window. 

Next, you will need to type “open” and then make sure that the “Display as icon” option remains unchecked. Depending on your settings, it may be checked or unchecked already. Lastly, you will need to click on “OK” at the bottom of the window.

The Next Steps

Once you have done all that, a new MS Word window will open up and it will be automatically titled “Document in [your file’s name].” 

Note: You may have to save the document before you can proceed if you are working with a blank document. If you are using a previously created and saved document, you will not have this issue.

Now that this second document is open, you can simply copy the source code from its original source and can be pasted directly into this newly created document. When you follow this process MS Word will automatically ignore all the spaces, tabs, and other formatting issues.

You will see a lot of spelling errors and grammatical errors highlighted in this document but once it is inserted into the original document, they will be ignored.

When you finish editing the source code document, simply close it and you will be prompted to save and confirm whether you want to insert it into the main document.

In Case You Missed Anything

Please note that while the process above seems quite daunting, the simplified steps are listed below.

  1. Click “Insert” tab on the ribbon
  2. Click “Object” OR Press “Alt + N then J” on your keyboard
  3. Click “OpenDocument Text”
  4. Type “open” (ensure “Display as icon” is unchecked)
  1. Click “OK”
  2. Copy and paste your source code into the new document
  3. Close the source code document
  4. Resume work on the main document.
Was this page helpful?