Formatting code with line numbers

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?

4 Answers

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.

2

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.

2

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.

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like