Oct 18, 2024
How to Insert Source Code Into a Word Document
Embed your code via a second document or use the Paste Special function This article explains how to insert code in Word. Directions apply to Word 2010 and newer, including Word 2021 and Microsoft
Embed your code via a second document or use the Paste Special function
This article explains how to insert code in Word. Directions apply to Word 2010 and newer, including Word 2021 and Microsoft 365.
While there isn't an 'insert code' option or a dedicated code block in Word, there is a way to insert one document into another. This effectively creates an isolated section that houses all the code. Here's how to make this work with your code:
These instructions only work with a single page of code.
Open the document you want the code to appear in, and select Insert.
In the Text group, select Object.
From the Create New tab, select Microsoft Word Document.
In Word 2007, select OpenDocument Text.
Clear the Display as icon check box.
Select OK.
A new document opens, titled Document in [target document file name]. Save the document in the same folder as the original document.
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.
Save and close the source code document. The code automatically appears in the main document.
You can't edit this object directly because it's really just an embedded document. Double-click the object to quickly open the document with the code.
Modern 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 your code-editing app will look different based on the paste type.
If you select Home > Paste Special, each of the three options yields a different result:
Programmers write software programs using languages like 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, despite missing a code block option, Word's document embed feature creates a container that protects source code from being reformatted.
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.
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.
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.
Get the Latest Tech News Delivered Every Day
Insert Object Microsoft Word DocumentOK Paste Special InsertText ObjectCreate NewMicrosoft Word DocumentOpenDocument TextDisplay as iconOKDocument in [target document file name]Home Paste SpecialUnformatted TextHTML FormatUnformatted Unicode TextHomeShow/HideFileOptionsDisplayShow all formatting marksOKLinkInsertLinksLinkFileSave AsSave as Type.html