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.
Asked
Active
Viewed 4.0k times
2 Answers
21
I found the following here.
Here is the best way, for me, to add code inside word:
- go to "insert" tab, click "object" button (it's on the right)
- choose "OpenDocument Text" which will open a new embedded word document
- copy and paste your code from Visual Studio / Eclipse inside this embedded word page
- 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.
-
Thanks for the answer @f_qi. But this is not exactly according to my needs. did you check the image with my post. – Zohaib Ahmad May 03 '17 at 20:05
-
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? – Priyal Jan 17 '18 at 06:03
-
How does one do this on the web version? – jazzmasterkc Mar 06 '20 at 15:48
-
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? – Mobeus Zoom Mar 20 '21 at 19:28
-
Only adds the first page of the "OpenDocument Text" – Marco Ramirez Castro Feb 16 '22 at 22:28
4
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.

Timothy Rylatt
- 7,221
- 2
- 10
- 14
-
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. – Wyck Nov 27 '17 at 14:32