How to Insert Code Snippets in Microsoft Word
Word isn’t designed to show off your code properly. Still, there are a few ways to insert code snippets in Word. This guide explains how.
There’s a reason that Microsoft Word is the go-to word-processing app for most people. It’s simple, easy to use, and has all of the features you might need when you’re creating documents. One thing that it isn’t great for, however, is code. While you could write your next Python app in Word, it isn’t something I’d recommend. That doesn’t mean you won’t ever need to insert code snippets in your Word document, though,
You might have a use case for sticking your code into a Word document, however. An obvious one is students, where you’ll need to paste some code snippets into your assignment write-ups. You can do this in a few different ways, as I’ll explain below.
Using the Keep Source Formatting Paste Option in Word
When you’re coding, you’ll use a certain type of formatting. One common example is indentation, which helps to make it easier for your code to be reviewed.
If you want to keep this formatting when you’re copying your code into Word, you can paste it by keeping the source formatting intact. This will keep the spacing, syntax highlights, indentations, and more. To paste code using the keep source formatting option, follow these steps.
- Copy the code into your clipboard. This might be from a website, text document, or an IDE you’re using.
- Next, open a new or existing Microsoft Word document and select where in the document you want the code to be pasted in.
- On the Home tab, click the drop-down arrow next to the Paste button and click Keep Source Formatting.
- You can also do this by right-clicking on the white page at the position you want to paste and selecting the Keep Source Formatting paste option instead.
Your code should now be pasted into the Word document with the original formatting intact.
Insert a Screenshot of Your Code Snippets into Microsoft Word
If you find that Word isn’t handling the formatting of your code too easily, you could try a different approach — using a screenshot. To insert a screenshot of your code, follow these steps.
- Open up your code, ready to be screenshotted.
- Next, open the Start menu and type Snipping Tool, then open the Best Match result.
- In the Snipping Tool window, select Rectangle from the Snipping mode options, then click New.
- Using your mouse, draw your screenshot area over your code.
- Your screenshot will appear in the Snipping Tool window. Click the Save As button to save the file.
- Next, open your Microsoft Word document and place your blinking cursor where you want to insert the screenshot.
- Next, select Insert > Pictures > This Device to begin inserting the image into Word.
- Browse to the location where your image file is saved, select the file, and then click Insert to add it to your document.
- The image will appear in your document. You can then resize it or move it, as required.
Using the Easy Code Formatter Third-Party Plugin for Word
If you don’t like the built-in options for adding code to your Word document, you could try a third-party tool instead. Easy Code Formatter is a third-party add-on for Word that helps you to preserve a lot of the formatting features you’d expect from your code, with syntax highlighting support for a lot of major programming languages built-in.
To install Easy Code Formatter, follow these steps.
- Open Word and select Home > Add-ins.
- From the drop-down menu, search for and select Easy Code Formatter.
- Click Add to begin the installation process.
- Click Allow and Continue in the panel on the right to complete the process.
- After installing Easy Code Formatter, you’re ready to format your code snippets. In your Word document, highlight the text you want to format as code. If it isn’t already inserted, paste it in as normal text — the add-in will sort out the formatting afterward.
- Select the Easy Code Formatter tab and select Format text as code.
- Easy Code Formatter will convert the code into a code snippet. It’ll identify the programming language used and apply the correct color scheme and syntax required for the code.
Formatting Code Correctly in Word
If you need to insert code snippets into your Word document, the steps above can help you get the job done without losing the formatting you need to keep it readable. A screenshot might be the quickest, but you won’t be able to edit it, so try pasting your code in while keeping the source formatting in place as you do it.
Got any other tips for handling code in Office apps like Word? Drop them in the comments below.