Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2006

Tip: Looking for answers? Try searching our database.

Save new documents in a loop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
EllenM - 07 Jan 2006 14:42 GMT
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
Doug Robbins - Word MVP - 07 Jan 2006 16:33 GMT
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

Signature

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

> Hello,
>
[quoted text clipped - 6 lines]
> Thank you in advance!!
> Ellen
EllenM - 07 Jan 2006 17:26 GMT
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

> What dictates how much text is to be included in each document?  The
> following will save each page as a separate document:
[quoted text clipped - 73 lines]
> > Thank you in advance!!
> > Ellen
Doug Robbins - Word MVP - 08 Jan 2006 08:56 GMT
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

Signature

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

> Thanks for your reply, Doug.  The following dictates what gets pasted into
> the new documents:
[quoted text clipped - 117 lines]
>> > Thank you in advance!!
>> > Ellen
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.