9

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 4

9

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
  • 1
    Thanks, worked nicely after running a replace of 000000 to remove the leading 0 form line numbers
    – Blem
    Commented Jan 16, 2012 at 13:59
  • Except that after inserting the line numbers, some of the syntax highlighting no longer works :( Commented Nov 20, 2015 at 9:35
7

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
  • What program is this?   I don’t see this in Word 2013. Commented Feb 12, 2020 at 3:36
  • This is in Notepad++, since it was already part of the work flow in the question.
    – MBraedley
    Commented Feb 12, 2020 at 12:18
4

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.
1
  • 1
    This is the correct answer for 2020+... it solves the problem as requested by OP using just Word.
    – LFLFM
    Commented Dec 5, 2022 at 23:49
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .