none
Trying to add a "line count" to the end of every Word document line including blank lines RRS feed

  • Question

  • Hello, i am trying to add a line number reference ie ".123" to the end of every line, blank line or not, and trying to replace the existing vbCr with a "." + lcnt + vbCr.

    But down below this vba code, this pgm seems to add on additional iterations to each line, please look below this code for the inputs and outputs.

    Sub LoopAllAssignLineNumRightSide()
    Dim lcnt As Integer
    lcnt = 0
    iSectCount = ActiveDocument.Sections.Count
    For i = 1 To iSectCount
         iParCount = ActiveDocument.Sections(i).range.Paragraphs.Count
         For j = 1 To iParCount
            iSent = ActiveDocument.Sections(i).range.Paragraphs(j).range.Sentences.Count
            For k = 1 To iSent
                iWord = ActiveDocument.Sections(i).range.Paragraphs(j).range.Sentences(k).Words.Count
                For L = 1 To iWord
                    txt = ActiveDocument.Sections(i).range.Paragraphs(j).range.Sentences(k).Words(L).text
                    If txt = vbCr Or sw = vbLf Or sw = vbCrLf Then
                       lcnt = lcnt + 1
                       ActiveDocument.Sections(i).range.Paragraphs(j).range.Sentences(k).Words(L).text = "." + CStr(lcnt) + vbCr
                    End If
                Next L
             Next k
         Next j
    Next i
    End Sub

    here is the test input: blank lines are where i just implanted "_" just for reference 

    _

    Line1 aaaa

    Line2 bbbbb

    _

    _

    Cccc

    _

    D

    here is the output:

    _

    .1

    Line1 aaaa.2.3

    Line2 bbbbb.4.5.6.7.8.9.10.11

    _

    _

    Cccc.12.13.14.15.16.17

    D.18.19

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    And i inititially just tried to deal with the sentence itself to avoid the slow iteration thru the Words, but that approach removed all the blank lines "_" and ended up with this output:

    .1

    Line1 aaaa.2

    Line2 bbbbb.3

    Cccc.4

    D.5.6

    from this code:

    Sub LoopAllAssignLineNumBySent()
     Dim lcnt As Integer
     Dim rmeol() As String
     Dim rngre As range
     lcnt = 0
     iSent = ActiveDocument
     iSectCount = ActiveDocument.Sections.Count
     For i = 1 To iSectCount
        iParCount = ActiveDocument.Sections(i).range.Paragraphs.Count
         For j = 1 To iParCount
            iSent = ActiveDocument.Sections(i).range.Paragraphs(j).range.Sentences.Count
            For k = 1 To iSent
                iWord = ActiveDocument.Sections(i).range.Paragraphs(j).range.Sentences(k).Words.Count
                iw = iWord
                lcnt = lcnt + 1
                rmeol = Split(ActiveDocument.Sections(i).range.Paragraphs(j).range.Sentences(k).text, vbCr)
                If rmeol(0) = "" Then
                   ActiveDocument.Sections(i).range.Paragraphs(j).range.Sentences(k).text = "." + CStr(lcnt) + vbCr
                Else
                   ActiveDocument.Sections(i).range.Paragraphs(j).range.Sentences(k).text = rmeol(0) + "." + CStr(lcnt) + vbCr
                End If
            Next k
         Next j
     Next i
    End Sub

    Thanks very much for any pointers and suggestions!

    bk


    rlk

    Wednesday, July 4, 2012 4:01 PM

Answers

  • If it's program code, we only need to look at paragraphs.

    The following macro adds "line numbers" reasonably fast.

    Sub NumberLines()
        Dim n As Long
        Dim m As Long
        Application.ScreenUpdating = False
        Selection.HomeKey Unit:=wdStory
    
        Do
            n = n + 1
            Selection.EndKey Unit:=wdLine
            Selection.TypeText "  ." & n
            m = Selection.Information(wdFirstCharacterLineNumber)
            Selection.MoveRight
        Loop Until Selection.Information(wdFirstCharacterLineNumber) = m
        Application.ScreenUpdating = True
    End Sub

    On my PC, it numbered 2784 lines (over 50 pages of source code) in 3.6 seconds.

    Regards, Hans Vogelaar

    Wednesday, July 4, 2012 7:17 PM
  • Excellent Hans! thanks very much and its super fast! and that's going to make a huge differenece because i have to process multiple thousands of programs!

    Ok super Hans! have a great week, really appreciate your time!

    This is a great forum!


    rlk

    • Marked as answer by bobk544 Wednesday, July 4, 2012 8:10 PM
    Wednesday, July 4, 2012 7:39 PM

All replies

  • Please excuse me, but adding text at the end of every line in a document doesn't make sense to me.

    Word automatically wraps text at the right margin. Adding text at the end of a line in the middle of a paragraph destroys the meaning of the paragraph, and is likely to be pushed to the next line, thereby changing the flow of the text.

    Why don't you use Word's built-in line numbering? Word's line numbers are displayed in the margin, and so aren't part of the text itself.


    Regards, Hans Vogelaar

    Wednesday, July 4, 2012 6:12 PM
  • Hello Hans thanks for the reply and the reason is that currently i've got the line numbers on the left for a static software xref system, but i'd like to move the line 231 pgm 70 to the right of the line if possible, but when i iterate at the WORD level, its very slow, so i'm also trying to append the ie ".231 70" at the Sentence level, but like you are saying, when i do this, it is somehow dynamically affecting the paragraphing or sentencing structures real time, so as i add the line number to the end, WORD is sensing that the sentence/word count has changed and is tripping up the loop counts ect. But i'm doing this mainly because these program copies will be static, ie what's in production, so once the line number is assigned it will become permanent and even if i move the line number around, that number will still be the final line # & pgm # 

    • .231 70                             'don't forget to check for cap     @b 22c113 52c113 <4> @e
    • .232 70                             Dim dCUSIP As New CUSIP  .1>63     @b 53s15  53s19  <4> 63c1 <5> @e
    • .234 70                             For Each dCpnStr In dCUSIP.CouponStructures   .259>63-

    Thanks Hans

    bk


    rlk

    Wednesday, July 4, 2012 6:47 PM
  • If it's program code, we only need to look at paragraphs.

    The following macro adds "line numbers" reasonably fast.

    Sub NumberLines()
        Dim n As Long
        Dim m As Long
        Application.ScreenUpdating = False
        Selection.HomeKey Unit:=wdStory
    
        Do
            n = n + 1
            Selection.EndKey Unit:=wdLine
            Selection.TypeText "  ." & n
            m = Selection.Information(wdFirstCharacterLineNumber)
            Selection.MoveRight
        Loop Until Selection.Information(wdFirstCharacterLineNumber) = m
        Application.ScreenUpdating = True
    End Sub

    On my PC, it numbered 2784 lines (over 50 pages of source code) in 3.6 seconds.

    Regards, Hans Vogelaar

    Wednesday, July 4, 2012 7:17 PM
  • Excellent Hans! thanks very much and its super fast! and that's going to make a huge differenece because i have to process multiple thousands of programs!

    Ok super Hans! have a great week, really appreciate your time!

    This is a great forum!


    rlk

    • Marked as answer by bobk544 Wednesday, July 4, 2012 8:10 PM
    Wednesday, July 4, 2012 7:39 PM