Copy Word lines into an array?

Q

quartz

I accidentally posted this question in another NG, please disregard the other
one.

I need an example function that will copy each line in a Word document
beginning at the first line and ending at the end of paragraph 10. Each line
needs to be separately copied into an array.

Can someone please, please, please post a function to do this? Thanks so
very much in advance.
 
G

Greg Maxey

Quartz,

Not sure I copy your intent.

Maybe:

Sub ScratchMacro()
Dim ListArray
Dim myRange
Dim i As Long
pos = ActiveDocument.Paragraphs(1).Range.Start
pos2 = ActiveDocument.Paragraphs(10).Range.End
Set myRange = ActiveDocument.Range(Start:=pos, End:=pos2)

ListArray = myRange.Text
ListArray = Split(ListArray, Chr(13))

For i = LBound(ListArray) To UBound(ListArray) Step 1
MsgBox ListArray(i)
Next
End Sub
 
Q

quartz

Greg, thanks so much for your post. Unfortunately, I need an exact line for
line from Word. The split function more or less yields a paragraph at a time.

I'm not sure if there is some way to loop through the paragraphs and copy
each line into succeeding array elements?

Any further ideas about this..?
 
G

Greg Maxey

quartz,

I am a VBA toddler and can't do much better than baby steps at present.
Could you spilt your array elements into individual paragraphs and then
increase the pos2 value?
 
J

Jean-Guy Marcil

quartz was telling us:
quartz nous racontait que :
I accidentally posted this question in another NG, please disregard
the other one.

I need an example function that will copy each line in a Word document
beginning at the first line and ending at the end of paragraph 10.
Each line needs to be separately copied into an array.

Can someone please, please, please post a function to do this? Thanks
so very much in advance.

Here is a "little" something that will work on a document containing at
least 10 paragraphs and no tables/textboxes... You would have to add some
tests/error trapping to make sure there are at least 10 paragraphs, if not,
modify the code to account for that (Especially the Set LineRange =
ActiveDocument.Paragraphs(10).Range line).

'_______________________________________
Dim StartRange As Range
Dim LineRange As Range
Dim LineArray() As String
Dim PageCount As Long
Dim myPage As Page
Dim myRectangle As Rectangle
Dim ParaCount As Long
Dim LineCount As Long
Dim i As Long
Dim j As Long
Dim k As Long
Set StartRange = Selection.Range

Application.ScreenUpdating = False

ParaCount = 1
k = 0
Set LineRange = ActiveDocument.Paragraphs(10).Range
LineRange.Collapse wdCollapseEnd
LineRange.Select

PageCount = Selection.Information(wdActiveEndPageNumber)

Selection.HomeKey wdStory

For i = 1 To PageCount
Set myPage = ActiveWindow.ActivePane.Pages(i)
Set myRectangle = myPage.Rectangles(1)
LineCount = myRectangle.Lines.Count
ReDim Preserve LineArray(k + LineCount)
j = 1
Do While ParaCount < 10
Set LineRange = myRectangle.Lines(j).Range
LineRange.Select
LineArray(k) = LineRange.Text
k = k + 1
j = j + 1
If j > LineCount Then Exit Do
ParaCount = ActiveDocument.Range(0, Selection.End).Paragraphs.Count
Loop
Next

ReDim Preserve LineArray(k - 1)

'Put array in new document
Dim newdoc As Document
Dim DocRange As Range
Dim m As Long

Set newdoc = Documents.Add
Set DocRange = newdoc.Range

For m = 0 To UBound(LineArray)
With DocRange
.InsertAfter LineArray(m)
.Collapse wdCollapseEnd
End With
Next

'Reselect original selection
StartRange.Select

Application.ScreenRefresh
Application.ScreenUpdating = True
'_______________________________________

Since I have just begun playing around with the Pages/Rectangles/Lines
collections, would someone with more experience with these collections
please comment on their usefulness/shortcomings?

TIA

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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