Macro to count lines and save in differents files

G

Guest

Hi all,

How to make a Macro to count 20 lines in a text with 3000 lines and save
every 20 lines in new file with total 150 new arquives starting in
File000.doc, file001.doc.., File150.doc?

I´ve tried to put a text with 2000 lines in word 2003 and start macro
count line 1 to line 20, file save as give a name file000.doc, start again
in line 21 go to line 40 filesave as give a name file001.doc. But nothing
work, this macro don't save the file.

Thank´s in advanced

jr
 
D

Doug Robbins - Word MVP

If these are just lines rather than paragraphs, there is a problem because
lines do not mean very much in Word.

If each line is actually a separate paragraph, then something like the
following would do it:

Dim i As Long
Dim Source As Document, target As Document
Dim arnge As Range
i = 0
For i = 0 To 150
Set Source = ActiveDocument
Set arnge = Source.Range
arnge.Start = Source.Range.Start
arnge.End = Source.Paragraphs(20).Range.End
Set target = Documents.Add
target.Range.FormattedText = arnge.FormattedText
target.SaveAs "C:\Test\File" & Format(i, "000")
target.Close
arnge.Delete
Next i

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Guest

Mr. Doug,

Thank you to help me.

The problem is to cut a text around 2000 or 3000 LINES (not paragraph)
indepent if line is blanck or not and save in a different files.

Example:

Open a text name GURU.doc

This text GURU.doc have 60 lines.

Cut this text and save this in number of files that contain 20 line each.

GURU01.doc start at line 00 and finish at line 20


GURU02.doc start at line 21 and finish at line 40


GURU03.doc start at line 40 and finish at line 59

Thank's in advanced
 
D

Doug Robbins - Word MVP

Try this:

Dim Totallines As Long
Dim Source As Document, Target As Document
Dim crange As Range
Set Source = ActiveDocument
Totallines = Source.Range.ComputeStatistics(wdStatisticLines)
lines = InputBox("Enter the number of lines that you want in each file")
If IsNumeric(lines) Then
For i = 1 To Int(Totallines / lines)
Source.Activate
Selection.HomeKey wdStory
Selection.MoveDown wdLine, lines, wdExtend
Set crange = Selection.Range
Set Target = Documents.Add
Target.Range.FormattedText = crange.FormattedText
Target.SaveAs "Document" & i
Target.Close
crange.Cut
Next i
Source.SaveAs "Document" & i + 1
Else
MsgBox "You must enter a whole number."
End If



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Guest

Hi!

I´ll try today and post the results tomorrow.

Thank´s for your help.

Jr

"Doug Robbins - Word MVP" escreveu:
 

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