How-To

How to Insert Code Snippets in Microsoft Word

How to Insert Code Snippets in Microsoft Word - fearured

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.

  1. Copy the code into your clipboard. This might be from a website, text document, or an IDE you’re using.
  2. Next, open a new or existing Microsoft Word document and select where in the document you want the code to be pasted in.
  3. On the Home tab, click the drop-down arrow next to the Paste button and click Keep Source Formatting.Pasting into Word while keeping the source formatting intact
  4. 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.
    Pasting into Word while keeping the source formatting intact

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.

  1. Open up your code, ready to be screenshotted.
  2. Next, open the Start menu and type Snipping Tool, then open the Best Match result.
    Opening the Snipping Tool on Windows
  3. In the Snipping Tool window, select Rectangle from the Snipping mode options, then click New.
    Creating a new screenshot using the Snipping Tool
  4. Using your mouse, draw your screenshot area over your code.
  5. Your screenshot will appear in the Snipping Tool window. Click the Save As button to save the file.
    Saving a screenshot in the Snipping Tool app
  6. Next, open your Microsoft Word document and place your blinking cursor where you want to insert the screenshot.
  7. Next, select Insert > Pictures > This Device to begin inserting the image into Word.
    Inserting a screenshot of code snippets into a Word document
  8. Browse to the location where your image file is saved, select the file, and then click Insert to add it to your document.
  9. The image will appear in your document. You can then resize it or move it, as required.
    An example of a screenshot image inserted into a Word document

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.

  1. Open Word and select Home > Add-ins.
  2. From the drop-down menu, search for and select Easy Code Formatter.
  3. Click Add to begin the installation process.
    Installing the Easy Code Formatter add-in in Word
  4. Click Allow and Continue in the panel on the right to complete the process.
    Confirm the installation of a Word add-in
  5. 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.
  6. Select the Easy Code Formatter tab and select Format text as code.
    Selecting code for formatting in Word
  7. 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.
    An example code snippet in Word using the Easy Code Formatter add-in.

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.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

 

To Top