Code Block in Word: 5 Ways to Insert Formatted Code
November 11, 2026 · 9 min read
Code Block in Word: 5 Ways to Insert Formatted Code
There's no button for a code block in Word, so you build one. The five working routes are a saved Code style, a one-cell table, pasting from VS Code, a formatter add-in, and converting Markdown to .docx. All of them keep code monospaced, but only some stop spellcheck squiggles and smart quotes from breaking it.
Below you'll find the steps for each method, the settings that protect code once it's in the document, and a table to help you pick. If you already write in Markdown, the Markdown to Word converter covers the last route.
Does Word Have a Built-In Code Block?
No. Word for Microsoft 365, Word for Mac and Word for the web have no code block object, no language picker and no syntax highlighting of their own. Every Word code block is a workaround built from ordinary formatting. Google Docs does have one on some Workspace editions, as our Google Docs code block guide explains.
A good one does five jobs at once:
- Monospaced font, so indentation and aligned columns line up.
- Shading or a border, so readers see where the snippet starts and ends.
- No proofing, so variable names don't get red and blue underlines.
- Straight quotes, because a curly quote breaks JSON, shell and most languages.
- Kept together, so a short snippet doesn't split across two pages.
The methods below differ in how many of those jobs they handle without extra work.
Method 1: Save a Reusable Code Style
A paragraph style is the cleanest Word code block for long documents. You set it up once, then apply it to any snippet with one click, and changing the style later restyles every snippet in the file.
- Type or paste a line of code and select it.
- Set the font to Consolas or Courier New at 9 or 10 pt.
- Right-click the selection, open Styles on the mini toolbar, choose Create a Style, name it Code and click Modify.
- Click Format > Paragraph. Set spacing before and after to 0, then on the Line and Page Breaks tab tick Keep lines together and Keep with next.
- Click Format > Border, open the Shading tab and pick a light grey fill.
- Click Format > Language and tick Do not check spelling or grammar.
- Select New documents based on this template so the style lands in Normal.dotm, then click OK.
Step 6 sets the style's no-proofing flag, which Microsoft's Style.NoProofing reference describes as making the checker ignore text in that style. On a Mac, the same Modify Style dialog opens from the Styles pane.
We prefer this method for anything longer than a couple of pages. Ten snippets styled by hand never match; ten snippets sharing one style always do.
Method 2: Wrap the Snippet in a One-Cell Table
A 1x1 table gives you a visible box with padding, and it travels well. It survives Word for the web, and it usually keeps its shape when someone opens the file in another word processor.
- Click Insert > Table and pick the single top-left square.
- Paste the code into the cell and apply your Code style, or set a monospaced font by hand.
- On the Table Design tab, use Shading to fill the cell with light grey.
- Right-click the table, choose Table Properties, open the Row tab and clear Allow row to break across pages.
Step 4 keeps a short snippet on one page. A snippet longer than a page still breaks, which is the honest limit of this approach.
The box also makes copying easy. Readers select the text inside the cell and copy the code without the table around it.
How Do I Paste Code Into Word Without Losing Formatting?
VS Code copies syntax colours to the clipboard by default. The setting is editor.copyWithSyntaxHighlighting, and it's on unless you've turned it off. Copy from the editor, press Ctrl+V (Cmd+V on a Mac) in Word, and the colours from your current VS Code theme come along. Run the VS Code formatter before you copy, so the indentation is consistent when it lands in Word.
If the paste arrives as plain text, try these in order:
- Click the Paste Options button that appears after pasting and choose Keep Source Formatting.
- Undo, then use Home > Paste > Paste Special in desktop Word and pick HTML Format.
- If you'd rather match the document, choose Keep Text Only and apply your Code style.
Option 3 loses the colours but gives you consistent snippets. For a quick one-off snippet with colour, options 1 and 2 give you the fastest Word code block.
Code Formatter for Word: The Easy Code Formatter Add-in
A code formatter add-in styles selected text as code inside Word. In Word for Microsoft 365 on Windows, open Home > Add-ins (in older versions, Insert > Get Add-ins), search for code, and install one.
The most-cited option is the Easy Code Formatter Word add-in, published by Arman Hilmioglu and still listed in Microsoft Marketplace as of November 2026. Its listing describes several open-source colour themes, a line rendering option and highlighting tools. It remembers your choices, so later snippets take one click.
Read the permissions before you install. The listing says the add-in can read and change your document and can send data over the Internet. Check your company's policy before you run proprietary code through any third-party add-in, and expect some workplaces to block add-ins entirely.
Stop Smart Quotes and AutoCorrect From Breaking Code
Word's AutoFormat As You Type rewrites characters while you type. Straight quotes become curly ones, and a curly quote in code is a syntax error. This JSON looks fine in Word but fails to parse:
{ “name”: “demo”, “debug”: true }
A parser rejects it because JSON only accepts the straight " character around strings. To stop the rewrite in Word for Windows, go to File > Options > Proofing > AutoCorrect Options. Open the AutoFormat As You Type tab and clear the straight quotes with smart quotes box. While you're there, consider clearing the hyphens-to-dash and hyperlink options too.
The setting applies to every document, so you'll lose curly quotes in prose as well. A safer habit is to write code in your editor and paste it, because pasted text isn't reformatted the way typed text is. If you need to pull code back out of a finished document, our Word to Markdown guide shows how to search for stray smart quotes.
Turn Markdown Code Fences Into a Word Code Block
If you draft in Markdown, write the snippet as a fenced block with a language tag. A converter then builds the Word formatting for you. Our Markdown code block guide covers the fence syntax in full. This is the input:
```python
def greet(name):
return "Hello, " + name
```
Pandoc turns that fence into a paragraph in its Source Code style and, per the Pandoc manual, highlights docx code by default:
pandoc notes.md -o notes.docx --syntax-highlighting=tango
The flag picks a colour theme. Older Pandoc releases call it --highlight-style, which the current manual lists as deprecated. To change the font or shading, edit the Source Code style in a reference document. Our Markdown to Word walkthrough explains that setup.
Our converter at /tools/md-to-docx runs on our server, and each export uses one of the 3 free daily operations shared by our server tools. The code arrives in Word as its own block, without syntax colours. You can draft the fence in our editor, where Ctrl+E (Cmd+E on a Mac) inserts a fenced code block.
Which Code Block in Word Method Should You Use?
| Method | Syntax colours | Effort per snippet | Best for |
|---|---|---|---|
| Saved Code style | No | One click | Manuals and long reports |
| One-cell table | No | About a minute | Snippets that must stand out |
| Paste from VS Code | Yes | One paste | One-off coloured snippets |
| Formatter add-in | Yes | One click | Frequent coloured snippets |
| Markdown plus Pandoc | Yes | None after setup | Docs written in Markdown |
All five keep the code as real, copyable text.
One limitation applies to all five: Word still doesn't know the text is code. It won't flag a syntax error, and nothing stops a colleague from retyping a quote and breaking a command. A screenshot avoids that, but readers can't copy from an image, so we keep screenshots for code nobody needs to run.
A code block in Word is always something you assemble, so pick the method that fits how often you do it. Use a saved style for long documents, a pasted VS Code snippet for quick colour, and Pandoc when your source is Markdown. If that's your workflow, draft the snippet in the editor and choose Export as DOCX when the document is ready.