Splitting a file

G

GaryT

I have a large file I need to split into multiple documents. After searching
online I found some code which will split a document into single pages.
However I need to ONLY split where there is a manual page break (creating
variable length documents), not at every page. If anyone can help this
absolute beginner by pointing me to directions for splitting at page breaks
I would greatly appreciate it.

Ideally I would also like to create the new file names based on the first
line of each new document. After splitting there will be about 200 documents
that I can rename manually if necessary, but suggestions for that automation
would be nice too.

BTW, the URL for the code that splits each page is found at
http://www.vbaexpress.com/kb/getarticle.php?kb_id=727

Gary T
 
G

Greg Maxey

Try:

Sub SplitIntoFiles()
Dim oDocSource As Word.Document
Dim oDoc As Document
Dim oRng As Range
Dim i As Long
Dim strNewFileName As String
Application.ScreenUpdating = False
Set oDocSource = ActiveDocument
Set oRng = oDocSource.Range
oRng.Collapse wdCollapseStart
Do Until oRng.End = oDocSource.Range.End - 1
i = oRng.End
oRng.MoveEndUntil Chr(12)
If oRng.End = i Then oRng.End = oDocSource.Range.End
Set oDoc = Documents.Add
oDoc.Range = oRng
strNewFileName = Left(oDoc.Paragraphs(1).Range.Text, 15)
oDoc.SaveAs strNewFileName
oRng.Move wdCharacter, 2
Loop
End Sub
 
G

GaryT

Almost. It creates the first document, then errors with run-time error 5487
when attempting to save the file. I believe it may have something to do with
how the first few lines are formatted. Each new section after the page break
begins with a page number, a name (centered), birth, and age, as shown here:

3

NIELS ANDERSON



Born: 1815 Denmark

Age: 41



Gary T





Try:

Sub SplitIntoFiles()
Dim oDocSource As Word.Document
Dim oDoc As Document
Dim oRng As Range
Dim i As Long
Dim strNewFileName As String
Application.ScreenUpdating = False
Set oDocSource = ActiveDocument
Set oRng = oDocSource.Range
oRng.Collapse wdCollapseStart
Do Until oRng.End = oDocSource.Range.End - 1
i = oRng.End
oRng.MoveEndUntil Chr(12)
If oRng.End = i Then oRng.End = oDocSource.Range.End
Set oDoc = Documents.Add
oDoc.Range = oRng
strNewFileName = Left(oDoc.Paragraphs(1).Range.Text, 15)
oDoc.SaveAs strNewFileName
oRng.Move wdCharacter, 2
Loop
End Sub
 
G

Graham Mayor

If, as seems to be the case, each new document is a separate section, then
the splitter macro at the end of
http://www.gmayor.com/individual_merge_letters.htm will split the document
at the section breaks. In fact if the document has been created from a
mailmerge, as seems likely, you could merge directly to separate documents
with the add-in from that web page.
If not, exactly what part of your document(s) do you want to use as the
filename(s) - bearing in mind that filenames cannot contain paragraph
breaks!

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

GaryT

The document was scanned (with permission from the author) from a book
containing information regarding individuals in a particular event. After
scanning, it was OCR'ed into a Word document. I have edited extraneous
material and added a manual page break after each person, and now need to
split them out so each person is in a separate file. I want (ideally) the
file name to be the number that begins each person's section. Note that the
number is the original page number printed in the book, not a real page
number that would be created by letting Word paginate the document. I tried
inserting a line break rather than a new paragraph but get the same error.
Basically I need each new document name to be something identifiable (such
as the number or the person's name on the next line) so I can cross check
with the index of the book to be able to sort files into various folders
(male, female, child, etc). If the file name could be created from any
characters on the first line before the paragraph or line break that would
be ideal. Or if I can be instructed how to format an acceptable first line
that the macro will not choke on I can scroll though the original and do
that. What is not as helpful is to use the original file name and just
increment it for the approximately 200 resulting documents, as I would have
to open each one to know where to sort. But if that is the only option I can
do that too.

I do appreciate all the help. I was asked to prepare this document and print
a single booklet of each person, but word processing is not my area of
expertise.

Gary
 
D

Doug Robbins - Word MVP

The document was scanned (with permission from the author) from a book
containing information regarding individuals in a particular event.
After scanning, it was OCR'ed into a Word document. I have edited
extraneous material and added a manual page break after each person, and
now need to split them out so each person is in a separate file. I want
(ideally) the file name to be the number that begins each person's
section. Note that the number is the original page number printed in the
book, not a real page number that would be created by letting Word
paginate the document. I tried inserting a line break rather than a new
paragraph but get the same error. Basically I need each new document
name to be something identifiable (such as the number or the person's
name on the next line) so I can cross check with the index of the book
to be able to sort files into various folders (male, female, child,
etc). If the file name could be created from any characters on the first
line before the paragraph or line break that would be ideal. Or if I can
be instructed how to format an acceptable first line that the macro will
not choke on I can scroll though the original and do that. What is not
as helpful is to use the original file name and just increment it for
the approximately 200 resulting documents, as I would have to open each
one to know where to sort. But if that is the only option I can do that
too.

I do appreciate all the help. I was asked to prepare this document and
print a single booklet of each person, but word processing is not my
area of expertise.

Gary
Run a macro containing the following code to replace the manual page
breaks with Section Breaks and then run splitter macro at the end of the
following page of fellow MVP Graham Mayor's website:

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

Doug Robbins - Word MVP

Sorry, here is the code that I omitted from the previous response:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="^m", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindContinue, MatchCase:=False) =
True
Selection.Delete
Selection.InsertBreak wdSectionBreakNextPage
Loop
End With


--
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, originally posted via msnews.microsoft.com
 
G

GaryT

Thanks to all for your help. I now have the individual stories split out
into their own documents. I will have to rename and sort them but that is
managable. The only oddity is that each one gives the following error when
opening the file:

Run-time error '91'
Object variable or With block variable not set

However if I click End the document opens and seems fine.

If that error is easy to fix that would be great, but if not I can live with
it for the few more times I will need to manipulate these files.

Gary
 
G

GaryT

Update - I deleted the two macros I no longer needed and the error has
disappeared.

Gary
 

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