none
WORD VBA Inserting text file's content into a WORD document and want to number the inserted text RRS feed

  • Question

  • Hello, i'm able to insert the contents from a text file but have been trying to insert a sequenctial line number onto each line of the the inserted text

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

    This is line 1 of my WORD document         (the code below inserts the text right after this only 1 line in my WORD doc)

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

      ad = "C:\_XREF\program1.txt"
      Selection.Collapse Direction:=wdCollapseEnd
      Selection.InsertFile FileName:=ad, Link:=True

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

    In this code i'm now trying to loop thru the doc with the inserted text and insert a line number on each line 

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

      Dim aword As Range
      Dim cnt As Integer
      For Each aword In ActiveDocument.Words
          If aword = vbCr Then
             cnt = cnt + 1

             aword = vbCr + cstr(cnt)

          End If
      Next
      
    End Sub

    Thanks for any help or suggestions on this!

    Friday, February 14, 2014 7:35 PM

Answers

  • Ok got it, super simple, thanks anyway!

    Public Sub InsertText1()

    Dim sr As String
    sr = Selection.Range.text

    ad = "C:\_XREF_BKP\WinMerge\Src\AboutDlg.cpp"

    Selection.Collapse Direction:=wdCollapseEnd
    Selection.InsertFile FileName:=ad, Link:=True

    Dim sent As Range
    Dim lc As Integer

    For Each sent In ActiveDocument.Sentences
        lc = lc + 1
        sent.InsertBefore (CStr(lc))
    Next

    End Sub

    • Marked as answer by bobk5444 Saturday, February 15, 2014 2:34 PM
    Saturday, February 15, 2014 2:34 PM