Formatting Issues

How to Add Formatted Code Snippets to Microsoft Word

Adam DavisAdam Davis Nov 3, 2026 870 views

Question details

The user wants to insert web-style code blocks with syntax highlighting and line numbers into Microsoft Word.

How to Add Formatted Code Snippets to Microsoft Word
Product
Microsoft Word
Device & OS
not provided
Scenario
Creating technical documents that require clear, readable developer code examples.
Observed behavior
Microsoft Word lacks a native code block feature, meaning users must rely on formatting workarounds or external editors to maintain syntax highlighting.
Before you start

Before proceeding, ensure you have an advanced code editor (such as Visual Studio Code or Notepad++) installed if you want to automatically retain accurate syntax highlighting when pasting code.

Solution 1Recommended

Copy and Paste from an Advanced Code Editor

The easiest way to preserve syntax highlighting and formatting is to copy your code directly from a dedicated editor.

Modern code editors automatically convert your code into Rich Text Format (RTF) when you copy it. This means the colors, spacing, and fonts are preserved when transferred to a word processor.

1
Write code in editor

Open a code editor like Visual Studio Code or Notepad++ and paste or write your code snippet.

2
Copy the formatted code

Highlight the code snippet and press Ctrl+C (or Cmd+C on Mac) to copy it to your clipboard.

3
Paste into Word

Open your Microsoft Word document, right-click the desired location, and select the 'Keep Source Formatting' paste option to insert the code with its original colors.

Copy and Paste from an Advanced Code Editor
Adding Background Shading: You might need to manually add a background color to the pasted text using the Shading tool in the Paragraph tab to make it look like a distinct web-style code block.
Free Microsoft Office alternative

Edit Technical Documents Seamlessly with WPS Office

While technical formatting can be cumbersome in traditional word processors, WPS Office provides a free, lightweight, and highly compatible environment for editing complex documents. Enjoy a familiar interface and seamless migration without paying for expensive subscriptions.

  1. 1. Download and Install: Get WPS Office for free from the official website and install it on your device.
  2. 2. Open Your Documents: Launch WPS Writer and open your existing .docx files directly without converting.
  3. 3. Format with Ease: Use the familiar formatting tools to effortlessly insert tables and text boxes for your code snippets.
Fully compatible with Microsoft Word (.docx), ensuring your custom code blocks and formatted tables remain intact.Free and lightweight Microsoft Office alternative with high performance.Familiar user interface for a zero-learning-curve transition.Robust built-in tools for formatting tables, text boxes, and typography.
microsoft office alternative - wps office

Frequently Asked Questions

Can I automatically add line numbers to code snippets in Word?

Word's built-in Line Numbers feature (found in the Page Layout tab) applies to entire document sections, not isolated blocks. To add line numbers just for a code snippet, you can use a two-column table and manually type the numbers in the left column.

Is there an add-in for formatting code in Microsoft Word?

Yes, you can explore third-party add-ins through the Microsoft Store (Insert > Get Add-ins). Search for 'code formatter' to find tools that can automatically apply syntax highlighting to plain text within your document.

Why does my syntax highlighting disappear when pasting into Word?

This usually happens if your source editor copies text as plain text instead of Rich Text Format (RTF), or if Word's default paste setting is forcing 'Keep Text Only'. Ensure your editor supports RTF copying and explicitly right-click to choose 'Keep Source Formatting' when pasting.