#1
|
|||
|
|||
Loop through each Line in Word
hi, i have a looping action for each Sentence and for each paragraph. But i would like to loop through each Line. is this possible?
Code:
Public Sub ParseLines() 'SPLIT BY PARAGRAPHS Dim singleLine As Paragraph Dim lineText As String For Each singleLine In ActiveDocument.Paragraphs lineText = singleLine.Range.Text MsgBox lineText Next singleLine End Sub |
#2
|
||||
|
||||
Being a word processor, Word doesn't really work in lines, then length and content of which can vary as a result of nothing more complicated than a change in the active printer. There are exceptions, of course, such as when lines are terminated by manual line breaks but even they can be affected by a change in the active printer (e.g. what was already the last of 10 words on a line before the manual line break might become the only word on that line). That's why paragraphs don't have a Line property. If you explain what you're trying to achieve, perhaps we can advise on how you might do so.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
I'm not intending to dispute Paul's reply. Word doesn't really work with lines.
There are however some methods and properties associated with the activewindow object that you might use. For example, using the following code, you can scroll through a document one line at a time: Code:
Option Explicit Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private bStop As Boolean Sub StopScroll() bStop = True End Sub Sub StartScroll() Dim i As Long Dim oPage As Page Dim j As Long Dim oRng As Word.Range bStop = False For Each oPage In ActiveDocument.ActiveWindow.ActivePane.Pages For i = 1 To oPage.Rectangles(1).Lines.Count j = oPage.Rectangles(1).Lines(i).Range.HighlightColorIndex oPage.Rectangles(1).Lines(i).Range.Select Set oRng = Selection.Range Selection.Collapse wdCollapseEnd oRng.HighlightColorIndex = wdYellow Doze 200 '= 1 second DoEvents ActiveDocument.ActiveWindow.SmallScroll Down:=1 'one line oRng.HighlightColorIndex = j Application.ScreenRefresh If bStop Then GoTo lbl_Exit Next i Next oPage lbl_Exit: Exit Sub End Sub Sub Doze(ByVal lngPeriod As Long) DoEvents Sleep lngPeriod End Sub |
#4
|
|||
|
|||
thanks for the script, ill tweak it and see if I can use it....
I figured I could loop "lines" and then do what i want with them since there are some basic functions Word provides that deal with lines, (line numbering) i am working on parsing text within Word for the internet so creating a cleanup program based on my personal needs (i have seen many clean up macros out there, they are great but dont fit for my needs) so i would like to isolate some "lines" that contain certain objects or formatting that I cant isolate with looping each "sentence". Could I loop each "line-breaks"? this is 1 example. I parsed sentences and this is the last word of the sentence and ended up in its own line. When I looked into it, it contains these 2 symbols. What are they whats their meaning in Word? (pic enclosed) |
#5
|
||||
|
||||
The ↵ characters in your document are manual line breaks. You can find them using:
Find = ^l For another 'cleanup' macro, try the one here: https://www.msofficeforums.com/word/...html#post32907. Although you say the ones you've tried don't do what you want, you haven't said what you see as their failings, so you're not giving yourself much chance of being helped with a solution.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Loop through Word doc to find and reformat text | Sandy27 | Word VBA | 5 | 01-06-2014 03:48 PM |
Loop action in Word until not found | kilburfi | Word VBA | 2 | 07-12-2013 01:26 AM |
How to a For loop in VBA | Jennifer Murphy | Word VBA | 1 | 01-29-2013 03:30 AM |
Macro to loop in Word | Yamaha Rider | Word VBA | 2 | 02-07-2012 05:33 PM |
WORD Macro - import picture - resize - position - page break - loop | Nano07 | Word VBA | 2 | 11-02-2011 05:14 AM |