Macro to separate one file into multiple files

D

Dan

I desparately need some help to take an existing document containing data for
multiple sales people, and break that file in to individual files at the
start of the new salesperson number. Can someone please help?
 
G

Greg Maxey

Dan,

This will need some grooming, but might get you started.

You this example assumes that each sales person is identified with a
four digit alphanumeric number S001, S002, etc.

To find and save the final number you will either need to include a
dummy last number or modify the code accordingly.

Sub ScrathMacro()
Dim i As Integer
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
i = 1
Do
With oRng.Find
.Text = "S" & Format(i, "000") & "*S" & Format(i + 1, "000")
.MatchWildcards = True
.Execute
If Not .Found Then Exit Do
oRng.MoveEnd unit:=wdCharacter, Count:=-4
oRng.Select
Documents.Add
With Documents(1)
.Range.InsertAfter oRng.FormattedText
.SaveAs "c:\S" & Format(i, "000") & ".doc"
.Close
End With
i = i + 1
'Redefine the range
oRng.Start = oRng.End
oRng.End = ActiveDocument.Range.End
End With
Loop
End Sub
 
D

Doug Robbins - Word MVP

If the document was created by mailmerge, you can probably use:

Sub splitter()
' splitter Macro
' Macro created by Doug Robbins to save each letter created by a mailmerge
' as a separate file, retaining the header and footer information.
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
Set Target = Documents.Add
Target.Range = Letter
Target.Sections(2).PageSetup.SectionStart = wdSectionContinuous
Target.SaveAs FileName:="Letter" & i
Target.Close
Next i
End Sub

Or, see the "Individual Merge Letters" item on fellow MVP Graham Mayor's
website at:

http://www.gmayor.com/individual_merge_letters.htm

If you are using Word XP or later, the "Add-in to Merge Letters to Separate
Files" that I have written and that can be downloaded from that site will
allow you to create each letter as a separate file with a filename taken
from a field in the data source with a minimum of fuss.


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

Dan

The file is one long running file with an indicator of the salesperson number
where the file should break.
 

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