Ways to insert a command or code block in Microsoft Word

If you are creating an article, instruction, or essay in Microsoft Word, you may need to include a code snippet. Whether it’s HTML, JavaScript, or Python, you probably want it to appear differently than the text in the document.

There are several ways to insert command or code blocks into your Word document. Depending on whether you want the reader to just be able to view the code or have the ability to copy it, we’ll walk you through the different options.

HOW TO INSERT A TABLE OF CONTENTS IN MICROSOFT WORD

Option 1: Paste Special in HTML

One of the fastest ways to add code to your document is the paste special option for HTML. This option inserts the code you copied without regard to the rest of the formatting in your document. Additionally, this option allows your reader to copy the code or command directly from your document.

Select the code or command in your application and copy it using the toolbar, context menu, or keyboard shortcut Ctrl+C on Windows or Command+C on Mac.

Copy to context menu

Place your cursor in your Word document where you want to paste it. Then go to the Home tab, click the Paste drop-down arrow, and choose Paste Special. Select “HTML Format” and click “OK”.

HTML format selected in the Paste Special box

When the code appears in your document, you can format the font or snippet if you wish.

HTML pasted into Word

Use the Home tab to color specific pieces of code with the Font section of the ribbon. To add a border or shadow, select the Borders drop-down arrow in the Paragraph section and choose “Borders and Shadows.”

Option 2: insert an object

If you want to insert the code or command into your document only so the reader can see it and not copy or edit it, you can insert an object containing the snippet.

Go to the Insert tab, click the Object drop-down arrow, and choose “Object.”

Object in the Insert tab

On the Create New tab, select “OpenDocument Text” as the object type. Click “OK”.

OpenDocument Text selected in Object box

A new Word document opens where you can insert your code or command. If you wish, you can use the Paste Special HTML format described above. After adding the snippet, close the document.

Your code then appears in your original Word document as an object. You can then move it, resize it, or add a border to it if you wish.

Option 3: attach a screenshot

Another option to add your snippet as an element instead of text is to use an image. If you have your code or command in an active application window like Notepad++ or Command Prompt, you can easily add it to your document. The downside is that the reader can’t easily copy your code when needed, and resizing can hurt readability.

Go to the Insert tab and click the Screenshot drop-down arrow. The application window should appear as an option.

Windows available in the image menu

Select it and it will be inserted into your document as an image. You can then crop the image to remove the surrounding application window if you wish.

You can also use the screen clipping option in the Screenshot drop-down menu. When you select this tool, your cursor turns into a crosshair. Use it to drag the area of ​​your screen or other application window you want to capture and release.

This image then appears in your document. You can move, resize, or customize the screenshot like any other image in Word.

Option 4: use a syntax highlighter

Another option is to use a syntax highlighter like Easy Syntax Highlighter. This is a free add-on for Microsoft Word that highlights your code, with a background and colors for parts of the code. Additionally, you can customize the language and appearance.

After installing the add-in, place the code or command in your document. You can type it or paste it, whichever you prefer.

Select the code and navigate to the “Easy Syntax Highlighter” tab that appears. To apply the default formatting with the automatically detected language, select “Highlight Selection” from the ribbon.

Highlighting the selection in the “Easy Syntax Highlighter” tab.

You’ll see your snippet highlighted and your text formatted.

Highlighted snippet with default theme

If you want to select a specific language or formatting, select “Settings” from the ribbon instead. When the sidebar opens, choose a language and theme.

Language and Theme in Settings

Once your code is selected, click “Highlight Selection” in the side menu.

Highlighting the selection in the sidebar

You will then see your snippet formatted according to the settings you have chosen. Your code or command remains as text for your reader to copy, but is highlighted in a separate block.

Snippet highlighted with dark theme

There is more than one way to add a block of code or command to your Word document. Depending on the purpose of your reader, choose the option that suits you best!

Leave a Comment