9

I am trying to add code snippets in Microsoft Word for my final thesis. The image below is the sample. I want to ask how to add these kind of code snippets in a word document.

enter image description

3 Answers 3

21

I found the following here.

Here is the best way, for me, to add code inside word:

  1. go to "insert" tab, click "object" button (it's on the right)
  2. choose "OpenDocument Text" which will open a new embedded word document
  3. copy and paste your code from Visual Studio / Eclipse inside this embedded word page
  4. save and close

The result looks very nice. Here are the advantages of this method:

  • the code keeps its original layout and colors
  • the code is separated from the rest of the document, as if it was a picture or a chart
  • spelling errors won't be highlighted in the code (this is cool !) And it takes only few seconds.
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer @f_qi. But this is not exactly according to my needs. did you check the image with my post.
How to do it for word 2017? OpenDocument Text is not available and the other options just create an icon for another document. Is there any way to add the code snippet preserving format?
How does one do this on the web version?
Why is this answer upvoted? The result looks nothing like what the OP asked for. Why did the OP select it in spite of complaining the same?
Only adds the first page of the "OpenDocument Text"
5

If you are simply looking to format text like your example apply the Block Text paragraph style. You may need to modify the style to add the background colour you want. Whilst modifying the style you can also tell Word not to check spelling or grammar for all instances of that style as shown below.

Modify Style Language dialog

1 Comment

Microsoft needs to add the ability to mark selected text as languages from other domains like "C++" or "JavaScript", etc. then add syntax highlighting features from Visual Studio Code or something.
1

You can add code blocks to Microsoft Word through an add-in, "Easy Code Formatter". https://appsource.microsoft.com/en-US/product/office/WA104382008?tab=Overview

Easy Code Formatter

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.