Directions apply to Word 2010 and newer, including Word 2021 and Microsoft 365.
This effectively creates an isolated section that houses all the code.
fire up the document you want the code to appear in, and selectInsert.

In theTextgroup, selectObject.
From theCreate Newtab, selectMicrosoft Word Document.
In Word 2007, selectOpenDocument Text.

Clear theDisplay as iconcheck box.
A new document opens, titledDocument in [target document file name].
Save the document in the same folder as the original document.

Copy and paste the source codeinto the new document.
Word automatically ignores the spaces, tabs, and other formatting problems.
Save and wrap up the source code document.

The code automatically appears in the main document.
you might’t edit this object directly because it’s really just an embedded document.
Double-opt for object to quickly bring up the document with the code.

Using Different Paste Types in Word
Modern versions of Word handle code better than they used to.
Word forMicrosoft 365supports 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 throw in.

To keep them on permanently, go toFile>Options>Display>Show all formatting marks>OK.
Alternatively, selectInsert>Links>Linkand enter the URL.
Toconvert a Word document to HTML, go toFile>Save As.
UnderSave as key in, choose.html.
you might also use an editor like Dreamweaver.