• finding lines with fewer characters (xp sr2)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » finding lines with fewer characters (xp sr2)

    Author
    Topic
    #432739

    We’re just setting up a book for the press, and we need to check that we do not have lines with spacing that is too wide (we distrust Word’s automatic hyphenation, and want to control it ourselves).

    Is there a routine that will enable us to find a line which has less than, say 50 characters?

    many thanks

    Viewing 1 reply thread
    Author
    Replies
    • #1015862

      But wouldn’t it be quite normal for the last line in a paragraph to have less than 50 characters?

      Here is a macro you could run on a copy of the document. It will highlight all lines with less than 50 characters (including punctuation)

      Sub TestShort()
      Const conLength = 50
      Dim rng As Range
      Dim rngPrev As Range
      Application.ScreenUpdating = False
      Selection.HomeKey Unit:=wdStory
      Set rng = ActiveDocument.Bookmarks(“line”).Range
      Do
      If rng.Characters.Count > 1 And rng.Characters.Count < conLength Then
      rng.HighlightColorIndex = wdYellow
      End If
      Set rngPrev = rng
      Selection.MoveDown Unit:=wdLine
      Set rng = ActiveDocument.Bookmarks("line").Range
      Loop Until rngPrev = rng
      Application.ScreenUpdating = True
      Set rng = Nothing
      Set rngPrev = Nothing
      End Sub

      • #1015867

        Dear Hans

        many thanks. But three further things:
        1. The line
        Set rng = ActiveDocument.Bookmarks(“line”).Range
        is provoking a 5941 error, and I’m told ‘the requested member of the collection does not exist’

        2. When I just shut the de###### down, I find that the job has been done, but only for the first page.

        3. If getting it to highlight is difficult, I could cope with its just finding the lines and moving the cursor there (though in that case I’d be glad if we could–as you say–avoid final lines of paragraphs).

        many thanks

        • #1015868

          The code worked ok for me in the multi-page documents I tried it on. Try single-stepping through the code to find out where it fails.

          • #1015879

            Thanks for this.

            a) is there a simple technique for single stepping, short of putting an apostrophe before each line and then gradually removing the apostrophes?
            I am now having the experience that the macro works for some documents but not for others. I think the reason is that if it comes across a footnote, it gets cross. It works fine on the book if I delete all the footnotes–and actually that will do for our purposes. (but if there were a simple way of a) avoiding the need to delete the footnotes excluding the last line (perhaps lines ending in a paragraph mark), it would of course be wonderful..

            best

            Philip

            • #1015882

              a) Click anywhere in the procedure and press F8. Each F8 will execute one step. You can inspect the value of variables by hovering the mouse over them.

            • #1015884

              Too difficult. Sorry.

            • #1015887

              many thanks indeed

              P

            • #1015920

              To skip the last line of each paragraph, replace the line

              If rng.Characters.Count > 1 And rng.Characters.Count < conLength Then

              with

              If rng.Characters.Count > 1 And rng.Characters.Count < conLength And _
              rng.Characters(rng.Characters.Count) vbCr Then

            • #1015917

              To skip over footnotes, add the following code

              Do While Selection.Information(wdInFootnote)
              Selection.MoveDown Unit:=wdLine
              Loop

              After the existing line

              Selection.MoveDown Unit:=wdLine

              StuartR

              Edited by StuartR to add
              This code will go into an infinite loop if the final line in the document is a footnote, but I guess you can check for that easily enough.

    • #1015937

      Have you tried selecting “do full justification like Word Perfect 6.x for Windows” from the tools/options/compatability tab? Because Word Perfect, like many layout programs, both reduces and expands word spacing, the result is more typographical. Word normally only expands word spaces when doing full justification. Try this out on a copy because your doc may be completely repaginated, particularly if you have widow and orphan control on.

      Pam

      • #1015965

        Thanks to everyone for these suggestions. I think we’re there!

        Pam–how does yours work if I’ve already got a document created? Do I just change the option, and assume it will happen?

        best wishes

        • #1015972

          Yes, you can change the full justification choice at any time (or change it back). In 2003 it will only apply to the document you selected it in, but it will stick with that document unless you change it.

          Pam

          • #1015983

            Just in case anyone is interested, here is an updated version of Hans’s original macro, with the two suggestions from Stuart.
            I’ve put in some not very subtle refinements:
            a) to enable you to specify the maximum number of characters in any line to be marked up
            to avoid the problem with it going into an infinite loop if there is a footnote on the last page.

            Sub TestShort()
            Selection.EndKey Unit:=wdStory
            Selection.InsertBreak Type:=wdPageBreak
            Selection.InsertBreak Type:=wdPageBreak
            Selection.TypeText Text:=”aaa”
            Dim CharacterNumber, Prompt1
            Prompt1 = “Below what number of characters do linee need to be marked up?”
            CharacterNumber = InputBox$(Prompt1)

            Dim rng As Range
            Dim rngPrev As Range
            Application.ScreenUpdating = False
            Selection.HomeKey Unit:=wdStory
            Set rng = ActiveDocument.Bookmarks(“line”).Range
            Do
            If rng.Characters.Count > 1 And rng.Characters.Count < CharacterNumber And rng.Characters(rng.Characters.Count) vbCr Then
            rng.HighlightColorIndex = wdYellow
            End If
            Set rngPrev = rng
            Selection.MoveDown Unit:=wdLine
            Do While Selection.Information(wdInFootnote)
            Selection.MoveDown Unit:=wdLine
            Loop
            Set rng = ActiveDocument.Bookmarks(“line”).Range
            Loop Until rngPrev = rng
            Application.ScreenUpdating = True
            Set rng = Nothing
            Set rngPrev = Nothing
            Selection.EndKey Unit:=wdStory
            Selection.TypeBackspace
            Selection.TypeBackspace
            Selection.TypeBackspace
            Selection.TypeBackspace
            Selection.TypeBackspace

            End Sub

    Viewing 1 reply thread
    Reply To: finding lines with fewer characters (xp sr2)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: