Splitting a range into lines

A

Andreas Baus

Hi. I am looking for a reliable way to split a given range in a Word
document into individual lines. That is, I am basically looking for a
function that takes a Range and returns a number of Ranges, each of
which represents a single line in the document (in it's current state
regarding formatting and textual alignment).
I tried a couple of different approaches, but was not really happy
with any of them, because they either seemed overly complicated or
left me doubtful about their robustness.
Therefore, if anyone can offer me any suggestions, I'd like to hear
them.
 
P

Peter Hewett

Hi Andreas Baus

When working with lines you must use the Selection object. You can assign it's
range to another range object or if you want to maintain a bunch of ranges use a
collection object.

Try the following code, it uses the current selection. It's start with the
first character selected but ends with the last whole line (as we are selecting
lines here):

Public Sub testme()
Dim colDL As Collection
Dim rngLine As Word.Range

Set colDL = LinesInSelection

For Each rngLine In colDL
Debug.Print rngLine.Text
Next
End Sub

Public Function LinesInSelection() As Collection
Dim rngLine As Word.Range
Dim rngEnd As Word.Range
Dim colLines As Collection
Dim boolDone As Boolean
Dim lngCount As Long

Set colLines = New Collection

' Start by selecting the first line
Set rngEnd = Selection.Range
Selection.Collapse wdCollapseStart
Selection.MoveEnd wdLine
Do
boolDone = EndOfDocument
Set rngLine = Selection.Range
lngCount = lngCount + 1
colLines.Add rngLine, CStr(lngCount)
'' Debug.Print lngCount; Tab(8); rngLine.Text

' Select next line
Selection.Move wdLine, 1
Selection.MoveEnd wdLine
Loop Until boolDone Or rngLine.End >= rngEnd.End

Set LinesInSelection = colLines
End Function

Private Function EndOfDocument() As Boolean
EndOfDocument = Selection.Type = wdSelectionNormal And Selection.End =
ActiveDocument.Content.End
End Function

HTH + Cheers - Peter


(e-mail address removed) (Andreas Baus), said:
 
D

Doug Robbins - Word MVP

Hi Andreas,

The following will split a selected range into individual lines by inserting
a vbCr at the end of each

Dim myrange As range, i As Long, lines As Long, rngedup As range
Set myrange = Selection.range
lines = myrange.ComputeStatistics(wdStatisticLines) - 1
For i = 1 To lines
Set rngedup = myrange.Duplicate
rngedup.Collapse wdCollapseStart
rngedup.Select
Selection.Bookmarks("\Line").range.InsertAfter vbCr
myrange.Start = myrange.Paragraphs(2).range.Start
Next i


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top