Save new documents in a loop

E

EllenM

Hello,

I’d like to cut and paste information from a large txt file into many
smaller txt files. What script will allow me to save the new smaller txt
files as the default name (document 1, document 2, etc.) in a loop? I’d like
these smaller files to be saved without my intervention.

Thank you in advance!!
Ellen
 
D

Doug Robbins - Word MVP

What dictates how much text is to be included in each document? The
following will save each page as a separate document:

Sub pagesplitter()

'

' splitter Macro

' Macro created 16-08-98 by Doug Robbins to save each page of a document

' as a separate file with the name Page#.DOC

'

Dim Counter As Long, Source As Document, Target As Document

Set Source = ActiveDocument

Selection.HomeKey Unit:=wdStory

Pages = Source.BuiltInDocumentProperties(wdPropertyPages)

Counter = 0

While Counter < Pages

Counter = Counter + 1

DocName = "Page" & Format(Counter)

Source.Bookmarks("\Page").Range.Cut

Set Target = Documents.Add

Target.Range.Paste

Target.SaveAs FileName:=DocName

Target.Close

Wend

End Sub

and the following will save each section as a separate document:

Sub sectionsplitter()

' splitter Macro

' Macro created by Doug Robbins to save each letter created by a mailmerge
as a separate file.

Dim i As Long, Source as Document, Target as Document, Letter as Range
Set Source = ActiveDocument
For i = 1 to Source.Sections.Count
Set Letter = Source.Sections(i).Range
Letter.End=Letter.End-1
Set Target = Documents.Add
Target.Range=Letter
Target.SaveAs FileName:="Letter" & i
Target.Close
Next i

End Sub


--
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
 
E

EllenM

Thanks for your reply, Doug. The following dictates what gets pasted into
the new documents:

After the 3500th line I do a find for the next blank line. Then I cut the
above text to the new text document. I'll keep cutting 3500 lines plus the
little extra, until the original document is empty.

I'm piecing it together somewhat:
To find the 3500th line:
Selection.GoTo _
What:=wdGoToLine, _
Which:=wdGoToAbsolute, _
Count:=3500

I don't know how to find blank lines, but the word "doctype" always follows
a blank, so I recorded the following:
Selection.Find.ClearFormatting
With Selection.Find
.Text = "doctype"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
Selection.Cut

I appreciate any help you can give me.

Thanks!!!
Ellen
 
D

Doug Robbins - Word MVP

What constitutes a blank line. When you click on the ¶ button, is it:

this is the end of a line.¶

This is the start of the first line after a blank line

--
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
 

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