4

I want to include an XML schema in a technical document created in MS Word. It has lines which are much longer than the available width on the page, so Word automatically breaks the lines. I want to make it explicitly clear to the reader where this has happened, so they know the next line is a continuation of the previous one. Is there a way to cause Word to automatically insert a symbol, for instance ⏎, at the end of each automatically broken line?

I could of course do it manually, but that would be a lot of work which I would have to repeat every time the schema changes, and it would be error prone. I know about the "show non-printing characters" mode, but I want these symbols to only be visible in this section, and to be printed as well. Besides, "show non-printing characters" shows the explicit line breaks instead of the automatic ones.

Is this possible with MS Word, or if not, can someone recommend another automated technique for achieving it?

  • Pressing ctrl+shft+8 will give you formatting symbols, which won't indicate exactly what you're looking for, but will indicate when a line is broken.. – Raystafarian Apr 30 '14 at 10:20
  • Yes, that's the "show non-printing characters" mode I mentioned. That doesn't work for my purpose, unfortunately. – Pepijn Schmitz Apr 30 '14 at 12:09
  • 1
    Does it have to be a special character? What about using Indentation->Hanging... This will auto-indent wrapped content (2nd line onward). – Madball73 Apr 30 '14 at 13:24
  • I would really prefer a special character, as it would be unambiguous. The file has indentation, so you would not always be sure if a line is meant to be indented, or a continuation of the previous line. – Pepijn Schmitz Apr 30 '14 at 14:18
  • Especially with a requirement that you show in all modes, I'm not sure this is possible, as word wrap location is not in a static location (i.e. it's not a line break, automatic or otherwise). Another option is to make it a bulleted list, or a table with shading, so that it is clear what is a discrete line. This would designate every line, not just wrapped ones though. – Madball73 May 1 '14 at 15:03
0

Well, you could try the following VBA as a starting point, but I think it will need some work. It assumes that your breaks are always going to be after a space (not unreasonable for a schema IMO) so if you have spaceless text longer than a line you might still have to break those by hand.

This replaces final spaces by the return symbol in the current font + a no-width breaking space, so as long as you can adjust the width of the return symbol (there are various possible ways to do that) so that Word still wraps at the same points, it may be enough.

Sub markAutoLineBreaks()
' Changes line breaks automatically made by Word
' into "return" charaters, but only where the line
' ends in a " "
' This operates on the text in the current selection
' We use a character style
Const strStyleName As String = "contchar"
Dim r As Word.Range
Dim styContchar As Word.Style

' Add the style if it is not present
On Error Resume Next
Set styContchar = ActiveDocument.Styles.Add(strStyleName, Type:=WdStyleType.wdStyleTypeCharacter)
Err.Clear
On Error GoTo 0
' Set the characteristics of the style. What you need to aim for
' is to adjust the character width so that the text breaks at the
' same point (if possible)
Set styContchar = ActiveDocument.Styles(strStyleName)
With styContchar.Font
  .Size = 8
End With

' Save the selection
Set r = Selection.Range

' remove old line end marks
With Selection.Find
  .ClearFormatting
  .Style = styContchar
  .Replacement.ClearFormatting
  ' Not sure what to use here, but this will have to do
  .Replacement.Style = ActiveDocument.Styles("Default Paragraph Font")
  ' 9166 is the return character. 8204 is a No-width breaking space
  .Text = ChrW(9166) & ChrW(8204)
  .Replacement.Text = " "
  .Forward = True
  .Format = True
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Moving down lines is not completely straightforward
' but this seems to work
Selection.Collapse direction:=wdCollapseStart
Do Until Selection.End > r.End
  Selection.Bookmarks("\line").Select
  If Right(Selection, 1) = " " Then
      Selection.SetRange Selection.End - 1, Selection.End
      Selection.Delete
      Selection.Text = ChrW(9166) & ChrW(8204)
      Selection.Style = styContchar
      Selection.Bookmarks("\line").Select
      Selection.Collapse direction:=wdCollapseStart
  End If
  Selection.MoveDown wdLine, 1, False
Loop

' reselect our original selection
r.Select
Set r = Nothing
End Sub
  • Intriguing. I want to try it out, but it doesn't compile. There are two "end ifs" in the do ... loop, and it's not immediately clear to me how you meant that. – Pepijn Schmitz May 7 '14 at 12:48
  • You're right. I've edited the code and just done a quick check. – user181946 May 7 '14 at 16:48
  • Thanks, now it works! It's not perfect yet. Sometimes it doesn't add the symbol to a broken line and it's not clear why. And sometimes it adds the symbol, but that causes the last word to no longer fit on the line so the last word plus the symbol get pushed to the next line. But it's a good start and probably as close as is possible to get to what I want, so I'll accept your answer. Can you think of a way to solve the issue with words getting pushed to the next line because the return symbol happens to make them just too long? – Pepijn Schmitz May 7 '14 at 18:11
  • I think only partial automation will be possible without full knowledge of Word's line-breaking algorithm and the ability to adjust the "return symbol" character's width accordingly. It would be possible to make the return symbol narrower - I'll look at that, but not today! The other thing I haven't looked at in detail is what happens to the no-width breaking space when you click the show/hide button and what difference that makes to what's displayed. – user181946 May 7 '14 at 19:58
  • OK, well this is already pretty useful to me. It gets it mostly right; fixing the few mistakes is a lot less work than doing it all by hand. Thanks! – Pepijn Schmitz May 8 '14 at 9:45
0

I won't put it as a duplicate, because it's not an answered question, but might want to look here:
Is it possible to set a style to show line continuations in Microsoft Word?

which leads you to : Convert HTML to image

  • Thanks for the pointer! I did search for duplicates, but didn't find that one. That won't work for me though; the code is much too long for an image of it to fit on one page. It would also be hard to get it to fit and flow naturally into the Word document, with the same fonts, font sizes, etc., and the resolution would have to be very high to still print nicely. It also sounds like much more work than doing it manually. ;) – Pepijn Schmitz May 1 '14 at 14:51

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.