Looking for a way to format my code in MS Word I found this question on Stack Overflow and combined the two high rated answers to create an openDocument with the code keeping its formatting:

  1. Go to "insert" tab, click "object" button (it's on the right)
  2. Choose "openDocument Text": it will open a new embedded word document
  3. Open code in Notepad++
  4. Select the programming language from the language menu
  5. In Notepad++ right click and select Plugin commands -> Copy Text with Syntax Highlighting
  6. Paste in to embedded word document
  7. save and close

This works good but I would like line numbers on the code, is there a way to do that?


When the code is in Notepad++, you can use TextFX->TextFX Tools->Insert line numbers to put line numbers in front of all of the necessary lines. Then when you copy/paste, the line numbers will be there.

| improve this answer | |
  • 1
    Thanks, worked nicely after running a replace of 000000 to remove the leading 0 form line numbers – Blem Jan 16 '12 at 13:59
  • Except that after inserting the line numbers, some of the syntax highlighting no longer works :( – Wang Dingwei Nov 20 '15 at 9:35

To avoid having to remove unnecessary leading zeros, you can use the Notepad++ column editor (Edit -> Column Editor or Alt+C) after you've done a column select at the first column. You'll get a window like this:

Column Editor Window

I like to use leading zeros, but you can just as well leave them out. Trailing spaces will be added instead. You will have to indent your code one extra time (or use a leading space), as this method won't automatically add one for you.

| improve this answer | |
  • What program is this?   I don’t see this in Word 2013. – G-Man Says 'Reinstate Monica' Feb 12 at 3:36
  • This is in Notepad++, since it was already part of the work flow in the question. – MBraedley Feb 12 at 12:18

I just conjured up a way that works amazingly for myself:

  1. Copy your code from your IDE and paste it into Word.  If your IDE has colors enabled for different parts of the program, you can paste with formatting to retain that.
  2. Highlight all text (Ctrl+A).
  3. In the Styles section of the Home tab, click the dropdown arrow and select create a new style.
  4. Give your style a name and click modify.
  5. Choose your basic formatting settings such as font, size, and spacing, etc.  I recommend Consolas as the font and your size is up to you.
  6. Select format in the lower-left corner of the window and click numbering, and select the format that you like best.
  7. Save your style and it should mirror your IDE almost exactly, ready to export or print as needed.
| improve this answer | |

if you have Notepad ++, you dont have to insert an opendocument.text - You can paste the syntax highlighted text directly in the word document.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.