none
foreach Word.Line ln in document.Range RRS feed

  • Question

  • I want to iterate each line in a document.  However I am not sure whether to use

                            

    foreach (Word.Range range in document.StoryRanges)
    
    {
    
     foreach (Word.Paragraph para in range.Paragraphs)
    
     {
    
    }
    }
    
    or
    
    foreach (Word.Line ln in document.Range
    
    {
    
    }


     


    Tuesday, August 16, 2011 1:01 PM

Answers

  • Hi Lugalzagesi

    I'm going to move this question to the Word for Developers forum because there's nothing in the VSTO technology that's going to help you with this. And you're going to need the assistance of object model specialists because Word doesn't have a "line" object. There's no way to easily loop through lines of text in Word. That's because lines, like pages, are completely dynamic entities that depend, moment to moment, on how the word processing engine lays out the document. A paragraph is always delimited by ANSI 13. Nothing marks the beginning or end of a line because it's completely virtual.

    The SELECTION object is the only one that gives you any access at all to Lines. You might want to investigate that while waiting for assistance in the Word for Developers forum...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Lugalzagesi Tuesday, August 16, 2011 4:15 PM
    Tuesday, August 16, 2011 1:26 PM
    Moderator
  • This routine will give you each line in the document:

    Sub WalkingTheLine()
      Dim doc As Word.Document
      Dim i As Long
      Set doc = Word.ActiveDocument
      Word.Selection.HomeKey unit:=wdStory
      For i = 0 To doc.ComputeStatistics(Word.WdStatistic.wdStatisticLines, True)
        Word.Selection.MoveDown unit:=wdLine, count:=1, Extend:=wdExtend
        Debug.Print Selection.Text 'convert this to what you want to do with the data
        Word.Selection.Collapse wdCollapseEnd
      Next
    End Sub
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by Lugalzagesi Thursday, September 1, 2011 12:15 PM
    Tuesday, August 16, 2011 8:57 PM

All replies

  • Hi Lugalzagesi

    I'm going to move this question to the Word for Developers forum because there's nothing in the VSTO technology that's going to help you with this. And you're going to need the assistance of object model specialists because Word doesn't have a "line" object. There's no way to easily loop through lines of text in Word. That's because lines, like pages, are completely dynamic entities that depend, moment to moment, on how the word processing engine lays out the document. A paragraph is always delimited by ANSI 13. Nothing marks the beginning or end of a line because it's completely virtual.

    The SELECTION object is the only one that gives you any access at all to Lines. You might want to investigate that while waiting for assistance in the Word for Developers forum...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Lugalzagesi Tuesday, August 16, 2011 4:15 PM
    Tuesday, August 16, 2011 1:26 PM
    Moderator
  • This routine will give you each line in the document:

    Sub WalkingTheLine()
      Dim doc As Word.Document
      Dim i As Long
      Set doc = Word.ActiveDocument
      Word.Selection.HomeKey unit:=wdStory
      For i = 0 To doc.ComputeStatistics(Word.WdStatistic.wdStatisticLines, True)
        Word.Selection.MoveDown unit:=wdLine, count:=1, Extend:=wdExtend
        Debug.Print Selection.Text 'convert this to what you want to do with the data
        Word.Selection.Collapse wdCollapseEnd
      Next
    End Sub
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by Lugalzagesi Thursday, September 1, 2011 12:15 PM
    Tuesday, August 16, 2011 8:57 PM