I'm using Word 2007 and inserting code into a document. I have a style with a fixed-width font and light grey background and all, and I use Notepad++ for syntax highlighting.

My problem is with lines of code that are too long to display. They are auto-wrapped, but then it's not obvious to see how exactly the lines are meant to be, i.e. where the newline actually is.

Is there a way to auto-insert an arrow symbol at the beginning of such wrapped lines to indicate that it is the same line (kind of like hyphenating, except on long lines instead of long words)?

So that, for example this:

public static void foo(String abcdefg, Boolean 123, String xyz)

(provided that the page/box/column/... is wide just enough to show text up to "123"), would be displayed like this:

public static void foo(String abcdefg, Boolean 123,
→String xyz)


I don't believe this is a feature in Microsoft Word, however I imagine you could emulate this by having a negative hanging indent, and then setting a background picture per paragraph such that the first 12pts worth of space is blank, and then the next 120pts is a 12pt high arrow, tiled 10 times. This would allow you to break over 11 lines.

| improve this answer | |
  • Just tried this, you can't set a background picture for a paragraph in Microsoft Word. – ta.speot.is Mar 26 '10 at 7:03

Perhaps not the answer you were looking for, but you could write some post-processing VBA code that scans for long lines and inserts the symbol you want.

This code might get you started (it is copy-paste ready):

Sub FixCodeLines()

Dim p           As Word.Paragraph
Dim c           As Word.Range
Dim charcount   As Integer
Dim MaxLineLen  As Integer
Dim txt         As String
Dim styname     As String

MaxLineLen = 68     ' number of characters on one line
txt = "    -->  "   ' symbol/text to insert
styname = "Code"    ' name of style to process

For Each p In ActiveDocument.Paragraphs
    If p.Style = styname Then
        charcount = 0
        For Each c In p.Range.Characters
            charcount = charcount + 1
            If (charcount Mod MaxLineLen = 0) Then
                c.InsertAfter Chr(11) & txt ' Chr(11) = soft enter
            End If
    End If

End Sub

The code will, for all the Code-styled paragraphs, insert a shift-enter and the text of your choice after each long line (based on the number of characters)

How to use:

  1. Apply the style "Code" to all your source code paragraphs (a good idea anyway not to use manual formatting). Make sure your Code style font is a fixed width font such as Courier.

  2. Copy-paste the code in the visual basic editor of Word (type ALT-F11, click Insert > Module, and paste the code)

  3. In your Word document, count the maximum number of characters on one line of your code code and change the value in the macro to that value (currently 68)

  4. To execute the macro, back in your Word document press ALT-F8, select FixCodeLines and press Run.

Note that you can only apply this macro only once, there is no intelligence to detect paragraphs that have already been 'fixed'. So you should run it at the very end of preparing your document.

Also, if you have a more recent version of Office (2003 etc), you might have some troubles saving/executing the document/macro because of security blabla. Read all potential Word dialog boxes with care to avoid losing your code.

| improve this answer | |
  • That may be a good idea, except that I don't know how to write VBA code. And would I have to highlight the text I want it to apply to, or even better have it applied to a certain style? – WaelJ Mar 26 '10 at 9:13
  • I've modified my answer with some example code. Indeed, you should definitely apply a style to your code paragraphs. It is better to set the light-grey background in the style (the option is a bit hidden though). It might be a good idea to reset any formatting by selecting your code and pressing ctrl-space and/or ctrl-q – Rabarberski Mar 29 '10 at 7:54

Another possible solution would be to add list numbering or bullet points to the paragraphs of code (add it to the code style). This would give you a simple indication of whether the line is wrapped or is new.

| 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.