How to Add Formatted Code Snippets to Microsoft Word
Question details
The user wants to insert web-style code blocks with syntax highlighting and line numbers into 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 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.
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.
Open a code editor like Visual Studio Code or Notepad++ and paste or write your code snippet.
Highlight the code snippet and press Ctrl+C (or Cmd+C on Mac) to copy it to your clipboard.
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.

Create a Custom Code Block using a Single-Cell Table
Use a 1x1 table to draw a distinct boundary around your code, allowing you to add custom shading and borders.
Submit a Feature Request to Microsoft
Since Word does not currently offer a native, dedicated code block feature, you can formally request it.
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. Download and Install: Get WPS Office for free from the official website and install it on your device.
- 2. Open Your Documents: Launch WPS Writer and open your existing .docx files directly without converting.
- 3. Format with Ease: Use the familiar formatting tools to effortlessly insert tables and text boxes for your code snippets.

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.




