Add code to word

You can also use SciTE to paste code if you don’t want to install heavy IDEs and then download plugins for all the code you’re making. Simply choose your language from the language menu, type your code, high-light code, select Edit->Copy as RTF, paste into Word with formatting (default paste).

SciTE supports the following languages but probably has support for others: Abaqus*, Ada, ANS.1 MIB definition files*, APDL, Assembler (NASM, MASM), Asymptote*, AutoIt*, Avenue*, Batch files (MS-DOS), Baan*, Bash*, BlitzBasic*, Bullant*, C/C++/C#, Clarion, cmake*, conf (Apache), CSound, CSS*, D, diff files*, E-Script*, Eiffel*, Erlang*, Flagship (Clipper / XBase), Flash (ActionScript), Fortran*, Forth*, GAP*, Gettext, Haskell, HTML*, HTML with embedded JavaScript, VBScript, PHP and ASP*, Gui4Cli*, IDL — both MSIDL and XPIDL*, INI, properties* and similar, InnoSetup*, Java*, JavaScript*, LISP*, LOT*, Lout*, Lua*, Make, Matlab*, Metapost*, MMIXAL, MSSQL, nnCron, NSIS*, Objective Caml*, Opal, Octave*, Pascal/Delphi*, Perl, most of it except for some ambiguous cases*, PL/M*, Progress*, PostScript*, POV-Ray*, PowerBasic*, PowerShell*, PureBasic*, Python*, R*, Rebol*, Ruby*, Scheme*, scriptol*, Specman E*, Spice, Smalltalk, SQL and PLSQL, TADS3*, TeX and LaTeX, Tcl/Tk*, VB and VBScript*, Verilog*, VHDL*, XML*, YAML*.

Code snippet in Word

If you’re creating an article, instructional piece, or essay in Microsoft Word, you may need to include a snippet of code. Whether HTML, JavaScript, or Python, you likely want it to appear different than the document text.

There are a few ways to insert command or code blocks in your Word document. Depending on if you want the reader to simply view the code or have the ability to copy it, let’s walk through the options.

Option 1: Paste Special as HTML

One of the quickest ways to add code to your document is with the Paste Special option for HTML. This inserts the code you’ve copied without the other formatting of your document. And, this option allows your reader to copy the code or command straight from your document.

RELATED: How to Paste Text Without Formatting Almost Anywhere

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

Copy in the shortcut 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 the snippet if you like.

Pasted HTML in Word

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

Shaded code block in Word

Option 2: Insert an Object

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

RELATED: How to Insert a Picture or Other Object in Microsoft Office

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

Object on the Insert tab

On the Create New tab, select “OpenDocument Text” as the Object Type. Click “OK.”

OpenDocument Text selected in the Object box

A new Word document will open for you to insert your code or command. You can use the Paste Special HTML format described earlier if you like. After you add 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 if you wish.

Object inserted in Word

Option 3: Attach a Screenshot

Another option for adding your snippet as an item instead of text is using 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 drawbacks here are that the reader can’t easily copy your code if they need to and that resizing may negatively affect readability.

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

Available windows in the image menu

Select it and it’ll pop into your document as an image. You can then crop the image to remove the surrounding application window if you like.

Inserted image in Word

Alternatively, you can use the Screen Clipping option in the Screenshot drop-down. When you select this tool, your cursor changes to a crosshair. Use it to drag the area of your screen or other application window you want to capture and release.

Screen Clipping tool capturing an image

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

Screen Clipping inserted in Word

Option 4: Use a Syntax Highlighter

One more option is to use a syntax highlighter like Easy Syntax Highlighter. This is a free add-in for Microsoft Word that highlights your code for you including a background and colors for pieces of the code. Plus, you can customize the language and appearance.

RELATED: How to Install and Use Add-ins for Microsoft Office

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

Select the code and go to the Easy Syntax Highlighter tab that now displays. To apply the default formatting with the language automatically detected, select “Highlight Selection” in the ribbon.

Highlight Selection on the Easy Syntax Highlighter tab

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

Snippet highlighted with default theme

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

Language and Theme in the Settings

With your code selected, click “Highlight Selection” in the sidebar.

Highlight Selection in the sidebar

You’ll then see your snippet formatted per the settings you picked. This keeps your code or command as text so your reader can copy it, but makes it stand out as its own block.

Snippet highlighted with dark theme

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

READ NEXT

  • › BLUETTI Slashed Hundreds off Its Best Power Stations for Easter Sale
  • › How to Adjust and Change Discord Fonts
  • › Google Chrome Is Getting Faster
  • › Expand Your Tech Career Skills With Courses From Udemy
  • › The New NVIDIA GeForce RTX 4070 Is Like an RTX 3080 for $599
  • › Mozilla Wants Your Feature Suggestions for Thunderbird

It can be done without taking snapshots of each segment

What to Know

  • To embed a second document into a Word document: Insert > Object > Create New > Word Document > clear Display as icon > OK.
  • You can also use Paste Special to insert a variety of data into a document, including code.

This article explains issues with using source code in Word for Microsoft 365, Word 2019, Word 2016, Word 2013, Word 2010, and Word 2007 and instructions for embedding a second document in a Word file. It also provides information on pasting other data into a Word document.

The Problem With Using Source Code in Word

Programmers write software programs using languages such as Java, C++, and HTML. Programming languages use different formatting and symbols from regular languages, so pasting a snippet of code into Word from a programming application causes errors such as text reformatting, indentation shifts, link creation, and misspellings.

Given how Microsoft Word structures documents, inserting and working with source code is more difficult than working in a dedicated code editor. However, document embeds create a container that protects source code from being reformatted.

One way to avoid these formatting issues is to paste the source code into a separate document within the main Word document.

Embed a Second Document Into a Word Document

Here’s how to paste source code into a Word document using a second embedded document.

These instructions only work with a single page of code.

  1. Open the target document in Microsoft Word and place the cursor where the source code will appear.

  2. Select Insert.

  3. In the Text group, select Object.

  4. In the Object dialog box, select the Create New tab.

  5. In the Object type list, Select Microsoft Word Document.

    In Word 2007, select OpenDocument Text.

  6. Clear the Display as icon check box.

  7. Select OK.

  8. A new document opens, titled Document in [target document file name]. Save the document in the same folder as the target document.

  9. Copy and paste the source code into the new document. Word automatically ignores the spaces, tabs, and other formatting problems. Spelling errors and grammatical errors are highlighted in the document, but these errors are ignored when the code is inserted in the original document.

  10. Save and close the source code document. The source code appears in the main document.

  11. Resume work on the main document.

Using Different Paste Types in Word

The most up-to-date versions of Word handle code better than they used to. Word for Microsoft 365 supports several forms of pasting, including with and without source formatting. So pasting a block of code from, for example, Microsoft Visual Studio Code will look different based on the paste type. If you select Paste Special, each of the three options yields a different result:

  • Unformatted Text: All code is pasted as unformatted, so you’ll lose indenting, color, typeface, and related contextual clues.
  • HTML Format: From VSC, a paste-as-HTML renders what appears to be a photo of the code, complete with the text editor’s background color. This code block is editable, and you can remove the background color in the Paragraph Fill menu option.
  • Unformatted Unicode Text: Pastes the text as is but strips the text and background colors. Re-format the code as needed.

FAQ

  • How do I see formatting marks and codes in Word?

    To see formatting marks and codes in Word temporarily, go to Home and select the Show/Hide icon to toggle marks on and off. To keep them on permanently, go to File > Options > Display > Show all formatting marks > OK.

  • How do I add links in Word documents?

    To add a link in a Word document, highlight the text you want to hyperlink, right-click it, and select Link. Alternatively, select Insert > Links > Link and enter the URL.

  • How do I convert a Word document to HTML?

    To convert a Word document to HTML, go to File > Save As. Under Save as Type, choose .html. You can also use an editor like Dreamweaver.

Thanks for letting us know!

Get the Latest Tech News Delivered Every Day

Subscribe

Hi fellow developer! In this article, we will answer the question: How do you Display Code Snippets in Microsoft Word?

The Sample code snippet we want to include in a Word document

Below, you can see an example of a code snippet to be included in our Word document:

SELECT *
FROM table1 t1
INNER JOIN table2 t2 ON t1.ID=t2.ID;
GO

The above code snippet is actually an SQL statement and we want to include it in a word document as a snippet.

How to include the code snippet in a Word document

The easiest way to include the above code snippet in a Word document, is to insert it as an object.

Therefore, in order to do this, you need to follow the below steps, within your Word document:

  • Navigate to the “Insert” tab in the Word ribbon (menu)
  • Click on “Object”
  • Select “OpenDocument Text” and click OK – a new Word window/doc launches:

How do you Display Code Snippets in Microsoft Word? - Article on essentialDevTips.com

  • Within the new Word window:
    • Type or paste your code with no spacing
    • Select all text and add a border (outside borders)
    • Select all text and add shading (grey for example)
    • Exit the document

How do you Display Code Snippets in Microsoft Word? - Article on essentialDevTips.com

  • After you perform the above, you can see that you will return to the original Word document, and this time, a formatted code snippet box with the text you pasted/typed, will be displayed.

How do you Display Code Snippets in Microsoft Word? - Article on essentialDevTips.com

Recommended Online Courses:

  • SQL Server 2022: What’s New – New and Enhanced Features
  • Introduction to Azure Database for MySQL
  • Working with Python on Windows and SQL Server Databases
  • Boost SQL Server Database Performance with In-Memory OLTP
  • Introduction to Azure SQL Database for Beginners
  • Essential SQL Server Administration Tips
  • SQL Server Fundamentals – SQL Database for Beginners
  • Essential SQL Server Development Tips for SQL Developers
  • Introduction to Computer Programming for Beginners
  • .NET Programming for Beginners – Windows Forms with C#
  • SQL Server 2019: What’s New – New and Enhanced Features
  • Entity Framework: Getting Started – Complete Beginners Guide
  • A Guide on How to Start and Monetize a Successful Blog
  • Data Management for Beginners – Main Principles

Read Also:

  • Understanding Dependency Injection in C#
  • Creating Responsive WinForms with BackgroundWorker in C#
  • How to Remove a Saved Skype for Business Password in Windows
  • How to Use a Percentage Formula in Excel
  • How To Show “This PC” Icon on Windows 10 Desktop
  • What is FinTech?
  • How to Write a “Hello World” App in Visual C++
  • How to Write a “Hello World” App in C#
  • How to Get Started with SQL Server – First Steps
  • Benefits of Primary Keys in Database Tables
  • How to Rebuild All Indexes Online for a SQL Server Database
  • What is the Internet of Things (IoT)?
  • Why Enforcing Regular Password Expiration is a Bad Practice
  • What is a Permalink?
  • Permalink Structures in WordPress

Reference: {essentialDevTips.com} (https://www.essentialdevtips.com/)

© essentialDevTips.com

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading…

Artemakis Artemiou

Artemakis Artemiou is a Senior SQL Server Architect, Author, a 9 Times Microsoft Data Platform MVP (2009-2018). He has over 15 years of experience in the IT industry in various roles. Artemakis is the founder of SQLNetHub and {essentialDevTips.com}. Artemakis is the creator of the well-known software tools Snippets Generator and DBA Security Advisor. Also, he is the author of many eBooks on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Moreover, Artemakis teaches on Udemy, you can check his courses here.

Post Views: 1,716

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit «Cookie Settings» to provide a controlled consent.

Microsoft Word is a very versatile software. It has a large number of great features which the common users don’t even know about. Let’s consider a scenario. The other day, Rajesh, a friend of mine sent me an email and told me that he was preparing an MS Word document and needed to embed programming code with syntax highlight. To insert code snippet in his MS Word document, Rajesh did all the syntax highlighting manually. You can imagine that it is a tedious job! So, he asked me if there was an easier way. Well, indeed there is one!

It is easy to embed code snippets with automatic syntax highlight in MS Word. Let’s learn how you can do this magic!

What is a Code Snippet and Syntax Highlighting?

A code snippet is a piece of computer programming code. Such code is usually marked with several colors in order to show various elements of the code distinctively. For example, all the programming language keywords will be highlighted in a particular color and all the errors will be highlighted in a different color. Similarly, there would be per-defined colors for text, tags, properties, attributes, values, symbols etc.

Our tips on MS-Word make your life easier and increase your productivity at work.

Our tips on MS-Word make your life easier and increase your productivity at work.

Program lines in a code snippets are also usually indented to make the code easily readable.

How to Insert Code Snippets with Syntax Highlight in MS Word

First of all, open the MS Word document in which you want to insert the code snippet. Then place the cursor where you want the snippet to be inserted.

Now, go to Insert tab and then click on the Object option. A new box will appear with a list of objects that you can insert in your MS Word document. Select OpenDocument Text from the list and click OK to insert the object.

You will see a new shaded area is inserted at the cursor position. Also a new Word document will open up. Whatever you will write or paste in this new document, the same will appear in the original document.

To add code snippets with syntax highlight, you can use Insert Object method of MS Word.

To add code snippets with syntax highlight, you can use Insert Object method of MS Word.

So, now, copy the code from your code editor and paste it in the new document. Save the new document and that’s it! Code snippet will appear neatly embedded in your MS Word document.

Syntax Highlighting is not Appearing in MS Word

If you have copied the code from a Microsoft product (like MS Visual Studio), the syntax highlight will automatically carry to the MS Word.

However, if you’re using some other code editors (like Programmer’s Notepad), copied code may appear without syntax highlight in MS Word. But don’t panic!

Most of the code editors will give you the option of copying text with HTML markup or RTF format. You should use these options while copying text.

In Programmer’s Notepad, this option appears as Edit -> Copy as RTF

In Notepad++ the option is available as Plugins -> Copy Text with Syntax Highlighting

If you’re copying syntax highlighted code from a webpage, the HTML markups will automatically get copied and syntax highlight will carry to MS Word.

Advantages of Embedding Code Snippets with Syntax Highlights

Although you can always manually highlight various portions of a code snippet in MS Word, but there are many distinctive advantage of using the embedded object method:

  1. Code and main document remain separate from each other (programmers love this approach, don’t they?)
  2. Spelling and grammar mistakes will not be highlighted by MS Word in the embedded code snippet. It is of great help because a lot of programming keywords are not found in dictionary, and as a result such code may result in lots of red and green wavy lines.
  3. And obviously, it’s easier, quicker method. Code remains in it’s original format and syntax highlights.
  4. Formatting changes in the main document do not affect the code block.

So, did you find this MS Word tips useful? Whenever you would need to insert a code snippet with syntax highlight in MS Word document, I am sure it will save you a lot of time. Please feel free to ask any questions on this tutorial. I will try my best to assist you. Thank you for using TechWelkin.

Download PC Repair Tool to quickly find & fix Windows errors automatically

In most cases, folks tend to use Notepad to add code blocks and commands, but if you already have Microsoft Word opened, then there is no need to fire up Notepad at all. From what we can tell, Microsoft Word is just as good, if not better, if you know how to paste commands and code blocks. let us see how to add Code Blocks and Commands in Microsoft Word.

Now, there are several ways to insert code blocks and commands to Word, but not a significant number. It all boils down to whether you want the reader to have the ability to copy the contents or just to view them. As expected, we are going to discuss how to accomplish each task. Worry not because everything will be easy to understand.

  1. Copy the code block or command
  2. Open Microsoft Word
  3. Paste content as HTML
  4. Insert code as an object
  5. Use Easy Syntax Highlighter

1] Copy the code block or command

Wherever you have the code block or command saved, you must copy the relevant content before we can send it over to Microsoft Word.

  • Press CTRL + A to highlight to the code.
  • From there, hit CTRL + C to copy everything.
  • Alternatively, use the keyboard to highlight a certain section of the code.
  • Then press CTRL + C to copy.

2] Open Microsoft Word

The next step to take here is to open a blank Microsoft Word document. This is where we will paste the code block or command recently copied.

  • Open the Word application from the desktop or via the Apps section.
  • From the main menu, please click on Blank Document.

You should now be looking at a blank document ready to be populated with content.

3] Paste content as HTML

How to add Code Blocks and Commands in Microsoft Word

If you want folks to copy the command or code block from your document, we suggest using the Paste Special as HTML feature. This is one of the fastest ways to add such content to a Word document as it inserts the code without using another formatting of the document.

  • Place the mouse cursor to the section where you want the code to appear.
  • Navigate to the Home tab.
  • After that, you must click on the Paste button found on the Ribbon.
  • Via the dropdown menu, please select Paste Special.
  • Select the option that reads, HTML Format.
  • Hit the OK button.
  • The code block or command should now be visible in HTML format.
  • Return to the Home tab to color the specified section of your code if you wish.

Easy, right? We agree.

4] Insert code as an object

Microsoft Word Object

This option is for those who want to share their code or command with others, but without giving the ability to copy. Viewers can only look, but cannot touch, though that does not stop them from taking screenshots.

  • Find the Insert tab in Microsoft Word
  • You must now click on the Object icon.
  • From the dropdown menu, select Object.
  • A new window known as Object should now appear.
  • From that window, please select OpenDocument Text.
  • Hit the OK button.
  • A new document window will load.
  • You can now paste the copied code block or command into this new document.
  • Finally, close the document.

If all goes according to plan, the code or command should show up in the original Word document as an Object.

5] Use Easy Syntax Highlighter

Highlight Selection Microsoft Word

If you want to highlight your code blocks and commands, then we suggest downloading the Microsoft Word add-in known as Easy Syntax Highlighter. Visit the official page to get it on your system right now.

  • Now that the add-in is up and running, paste the code block or command.
  • Select the command or code.
  • From there, go to the Easy Syntax Highlighter tab.
  • In order to deploy the default formatting, click on Highlight Selection.
  • Your snippet will be selected and text formatted.

Read: How to Warp and Convert multiple Words to one Shape in Illustrator

How do I insert HTML code into a Word document?

Inserting HTML codes into a Word document is a simple task if you know what you’re doing.

  • All you have to do is open a blank Word document.
  • Got to the Insert tab.
  • Navigate to the Text section.
  • Click on the Object icon.
  • Select Text From File.
  • Take advantage of the file type selector from the Insert File box.
  • Choose the All Web Pages option.
  • Convert File box should now appear.
  • Select Other Encoding.
  • Finally, hit the OK button.

Can you write code in Microsoft Word?

You can write whatever you want in Microsoft Word because after all, it is a word processing tool. Still, tools such as Notepad will always be best for writing code due to their simplicity and specialized features.

Ezoic

Vamien has studied Computer Information Services and Web Design. He has over 10 years of experience in building desktop computers, fixing problems relating to Windows, and Python coding.

Visualizzare frammenti di codice sorgente in Microsoft Word con Syntax Highlighting

Finding a way to decently show our source code on the web recently became a rather trivial task thanks to many JavaScript + CSS based libraries such as highlightjs, PrismJS, Prettify, SyntaxHighlighter & more. It’s not so easy to achieve the same results inside a text-editing software such as MS Word, to the point that I’ve seen more than few devs adopting the awful & dreadful screenshot method, which consist in blindly capture a portion of their developer framework GUI and then embedding it as an image inside the Word document.

Should we do that? Of course no! There is at least one better way to do that, which also is far less intrusive (and size-intensive) and also gives us the chance to modify/review our code snippet after it has been pasted. Let’s see how we can do it.

  • Open MS Word and place the cursor where you want to paste the snippet.
  • Go to the Insert tab, from there click to the Object… button close to the right-end of the toolbar. A popup will show up: select OpenDocument Text.
  • A new «embedded» document will appear: go to your favourite Developer Framework, such as Visual Studio, Eclipse, Android Studio, Notepad++ or the one doing the highlight the way you want it to be. Copy the code from there and paste it to the new document.
  • Save and close the new document.

Your efforts will be rewarded with something like that:

code-snippet-in-ms-word

This solution, other than being visually nice, grants you the following benefits:

  • the source code will be entirely editable.
  • all the layout, coloring, spaces, tabs, linefeeds from the developer framework will be kept.
  • the code will be treated like a separated, embedded element, just like images and charts, so you’ll be able to move, resize, shape, applying borders, shadows, background color and basically anything else you want to and/or Word allows.
  • the code won’t be affected by grammar checker, meaning that spelling errors won’t be highlighted in the code: no red underlining, yay!
  • it takes only few seconds to do it  — you’ll love that if you need to insert multiple snippets.
  • It works on all Word builds since 2007: Word 2007, Word 2010, Word 2013 and newer versions.

If you find an even better way, please let me know.

Meanwhile… Happy coding!

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it’s also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Word does not natively support syntax highlighting, or other code formatting. As such, it is best to create the highlighting etc in a different program (visual studio, eclipse, notepad++, or this website which can do 14 languages and requires no install).

When pasting the text however, doing it directly in the word document itself can lead to headaches like spelling and grammar underlines.

To fix this, instead of directly copying and pasting, use Insert -> Object -> OpenDocument Text. This will open a new document. Paste the text here, and close the window. Your code will now be shown as an object, complete with all of the formatting that showed in the previous window, but with no spelling or grammar check showing.

While it is possible to format code with styles, there are no built in styles to do this, and it is not really what styles are meant to do. If you with to use styles to format your code, just use this style in all of the objects that you create.

Update: If you want to do this with code styles, here are some ideas.

  • Use a paragraph style with a monospaced font
  • Light background (grey works well)
  • Spelling turned off
  • Make sure the line spacing is the way you want it

If you want to add a little more complexity, you can layer character styles on top of the paragraph style to create something similar to the code preview on stackoverflow with different formatting and/or color for class names, instance names, etc.

Setting up the style like this takes work, but you can get a more customizable result than compared to what you can get using the syntax highlighting from notepad++ or Visual Studio.

As a speedup when formatting code with a style, it may help to first do the syntax highlighting in one of those programs, and then use Word’s Find utility to search for text that matches a color or other font style so that you do not have to manually identify what is a class name for example in the code manually (it is under more->format in the find prompt).

Like this post? Please share to your friends:
  • Add cells in vba excel
  • Add cell range in excel
  • Add borders in excel
  • Add bits in a word
  • Add approve it to word