How to Insert Code Blocks and Commands in Microsoft Word

338191 How to Insert Code Blocks and Commands in Microsoft Word

Inserting code blocks and commands into Microsoft Word documents allows you to share code snippets and instructions, while preserving formatting. However, Word is optimized for flowing text, not code, so special techniques are required.

In this guide, you’ll learn several methods to insert code and commands into Word documents, including:

  • Pasting as HTML
  • Using text boxes
  • Embedding documents
  • Installing syntax highlighters

Why Insert Code in Word Documents?

You may need to insert code blocks or commands in Word for various reasons:

  • Writing technical documentation or tutorials
  • Sharing code examples with non-developers
  • Preserving syntax highlighting when presenting code
  • Allowing readers to easily copy and paste code snippets

Without special formatting, pasted code loses indentation, colors, and other elements that help it remain readable. The techniques covered below aim to maintain code formatting.

Method 1: Paste Special as HTML

The quickest way to add code to Word documents is using Paste Special as HTML. This pastes copied code without applying Word’s usual text formatting. It also allows readers to copy the code.

To use Paste Special for code:

  1. Copy the code snippet from its source application
  2. In Word, place the cursor where you want the code inserted
  3. Go to Home > Paste > Paste Special
  4. Select HTML Format and click OK

The code will now appear formatted as HTML, allowing it to stand out from regular text.

Method 2: Insert as Text Box

You can also insert code into a text box. Text boxes isolate content, preventing Word from reformatting it. Readers can also copy text box content.

To use a text box for code:

  1. Go to Insert > Text Box
  2. Draw a text box by dragging your mouse
  3. Paste or type your code inside the box
  4. Resize the box as needed

Format the text box using options on the Format tab to give it borders, colors, etc.

Method 3: Embed Another Document

For larger code samples, embed another Word document inside your main one. The embedded document creates a protective container for code.

To embed a document:

  1. Insert > Object > Create New
  2. Select Microsoft Word Document
  3. Click OK to create the document
  4. Paste your code inside and save it
  5. Resize the embedded document as needed

Method 4: Use a Syntax Highlighter

For the best-formatted code, install an Office add-in syntax highlighter. These highlight code elements like keywords, variables, and comments.

Popular syntax highlighters include:

To use a syntax highlighter:

  1. Install the add-in and restart Word
  2. Select your code and apply highlighting

Formatting Code Blocks in Word

Whichever method you use, consider visually formatting code blocks for readability:

  • Monospace font: Use Courier New, Consolas, etc.
  • Color coding: Keywords orange, strings green, etc.
  • Borders: Add border lines around code blocks
  • Line numbers: Display line numbers for easy reference
  • Spacing: Add space before and after code blocks


Inserting readable code blocks into Word takes just a few easy steps. By pasting as HTML, using text boxes, embedding documents, or installing a syntax highlighter – you can integrate code while maintaining its formatting.

Highlighting code elements also helps readers quickly parse code examples in documents. With the right setup, Microsoft Word can be an excellent tool for sharing code snippets alongside explanatory text.

About The Author