split word doc into several files

A

abqhusker

Pls help. Once a month I receive a document that I have to trun
around and divide into several 10 page individual files. For example,
the word document is 100 pages long, the pages are numbered 1-10 and
then repeat 1-10 for the next 10 and so on. there are manual page
breaks at the end of every page, and the margin are 0.5" all the way
around, am using Word 2007. Does anyone have a code or is there a way
to save every 10 pages as it's own file? I've tried some code that
I've found using google but I don't know enough to know how to edit
the code to make it work the way I'm wanting. The one code I found
that worked best so far is one that makes every page it's own file,
but that's not good for the document I'm working with because every 10
pages is for one customer.
 
K

Klaus Linke

A quick way to split it would be via the "Master document" feature:

Is there already a heading ("Heading 1", or "Heading 2, or ...) at the top
of each of your ten sections?
If not, put one in, with a sensible name (which will be used as the file
name) at the top of each one.

Select everything.

Then in the "Master document" view (accessible from Outline view through the
button on the right), use the button to create subdocuments.

Now save... and the ten subdocuments will be in the location you saved the
master document.
You can throw away the empty master document.

Regards,
Klaus
 
K

Klaus Linke

You can throw away the empty master document.

I'm assuming you have a backup of the original, before you try any of
this...
Don't want you to lose your work!

:) Klaus
 
P

Pesach Shelnitz

Try this.

Sub SplitDocAndSaveParts()
Dim myRange As Range
Dim doc As Document
Dim name, partName As String
Dim i, j, k As Integer
Dim pos1, pos2 As Variant
Dim endFound As Boolean

endFound = False
name = ActiveDocument.FullName
k = InStr(1, name, ".")
name = Left(name, k - 1)
Selection.HomeKey wdStory
Do While endFound = False
pos1 = Selection.Start
For j = 1 To 10
ActiveDocument.Bookmarks("\Page").Select
If ActiveDocument.Bookmarks("\Page").Range.End _
<> ActiveDocument.Bookmarks("\EndOfDoc").Range.Start Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Else
endFound = True
Exit Do
End If
Next
Set myRange = ActiveDocument.Range(Start:=pos1, End:=Selection.Start)
myRange.Copy
Set doc = Documents.Add(ActiveDocument.AttachedTemplate.FullName)
Selection.Paste
doc.SaveAs fileName:=name & "_Part" & CStr(i),
FileFormat:=wdFormatDocumentDefault
doc.Close
i = i + 1
Loop
Set doc = Nothing
Set myRange = Nothing
End Sub
 
A

abqhusker

Try this.

Sub SplitDocAndSaveParts()
    Dim myRange As Range
    Dim doc As Document
    Dim name, partName As String
    Dim i, j, k As Integer
    Dim pos1, pos2 As Variant
    Dim endFound As Boolean

    endFound = False
    name = ActiveDocument.FullName
    k = InStr(1, name, ".")
    name = Left(name, k - 1)
    Selection.HomeKey wdStory
    Do While endFound = False
       pos1 = Selection.Start
       For j = 1 To 10
            ActiveDocument.Bookmarks("\Page").Select
            If ActiveDocument.Bookmarks("\Page").Range.End _
               <> ActiveDocument.Bookmarks("\EndOfDoc").Range.Start Then
               Selection.MoveRight Unit:=wdCharacter, Count:=1
            Else
                endFound = True
                Exit Do
            End If
        Next
        Set myRange = ActiveDocument.Range(Start:=pos1, End:=Selection.Start)
        myRange.Copy
        Set doc = Documents.Add(ActiveDocument.AttachedTemplate..FullName)
        Selection.Paste
        doc.SaveAs fileName:=name & "_Part" & CStr(i),
FileFormat:=wdFormatDocumentDefault
        doc.Close
        i = i + 1
   Loop
   Set doc = Nothing
   Set myRange = Nothing
End Sub
--
Hope this helps,
Pesach Shelnitz





- Show quoted text -

The master document feature does not work for me because I don't have
any room to spare on the document, e.g. when I apply the heading 1 to
the date on the first page, it pushes the last 2 or 3 lines of text on
the 10th page over on to an 11th page and that's not acceptable. I
tried the macro but I get a comile error: syntax error but not sure
what's wrong. The top line is highlighted yellow and the two lines
towards the bottom are in red. I am pasting here in case it's an easy
fix. I appreciate you both offering to help and if you have any other
ideas, please let me know. Here's the code:

Sub SplitDocAndSaveParts()
Dim myRange As Range
Dim doc As Document
Dim name, partName As String
Dim i, j, k As Integer
Dim pos1, pos2 As Variant
Dim endFound As Boolean


endFound = False
name = ActiveDocument.FullName
k = InStr(1, name, ".")
name = Left(name, k - 1)
Selection.HomeKey wdStory
Do While endFound = False
pos1 = Selection.Start
For j = 1 To 10
ActiveDocument.Bookmarks("\Page").Select
If ActiveDocument.Bookmarks("\Page").Range.End _
<> ActiveDocument.Bookmarks("\EndOfDoc").Range.Start
Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Else
endFound = True
Exit Do
End If
Next
Set myRange = ActiveDocument.Range(Start:=pos1,
End:=Selection.Start)
myRange.Copy
Set doc = Documents.Add
(ActiveDocument.AttachedTemplate.FullName)
Selection.Paste
doc.SaveAs fileName:=name & "_Part" & CStr(i),
FileFormat:=wdFormatDocumentDefault
doc.Close
i = i + 1
Loop
Set doc = Nothing
Set myRange = Nothing
End Sub

This part is the part that's red when I try to run the code:
doc.SaveAs fileName:=name & "_Part" & CStr(i),
FileFormat:=wdFormatDocumentDefault

and this part is highlighed yellow:
Sub SplitDocAndSaveParts()

Thanks.
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :

This part is the part that's red when I try to run the code:
doc.SaveAs fileName:=name & "_Part" & CStr(i),
FileFormat:=wdFormatDocumentDefault

Red means the compiler cannot "understand" a particular line.

In this case, it is because those two lines should be on one line, or, if
you want them on two line, use the line separator thusly:

doc.SaveAs fileName:=name & "_Part" & CStr(i), _
FileFormat:=wdFormatDocumentDefault
and this part is highlighed yellow:
Sub SplitDocAndSaveParts()

The line highlighted in yellow is usually the line that gets the compiler to
stumble due to an error. In this case, because the Sub cannot be compiled
(You have a red line above), the compiler highlights the first line because
it could not even start compiling the code, let alone even execute it.
 
P

Pesach Shelnitz

Hi,

As Jean-Guy explained, the compile error was due to the fact that a line in
my macro was split into two lines. This happened when I copied the macro into
the Reply form. Unfortunately, in my haste I didn't notice and fix this, so
you ended up having the compile error and some frustration. Please accept my
apology.

Meanwhile, I fixed that problem and also made a couple of additional
refinements so that the macro will work even better. Note that the original
doc should be a Word2007 doc with the .docx extension. Please try this fixed
version and let us (the newsgroup) know how it works.

Sub SplitDocAndSaveParts()
Dim myRange As Range
Dim doc As Document
Dim name, partName As String
Dim i, j, k As Integer
Dim pos1 As Variant
Dim endFound As Boolean

endFound = False
name = ActiveDocument.FullName
i = 1
k = InStr(1, name, ".docx")
name = Left(name, k - 1)
Selection.HomeKey wdStory
Do While endFound = False
pos1 = Selection.Start
For j = 1 To 10
ActiveDocument.Bookmarks("\Page").Select
If ActiveDocument.Bookmarks("\Page").Range.End _
<> ActiveDocument.Bookmarks("\EndOfDoc").Range.Start Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Else
endFound = True
Selection.Collapse Direction:=wdCollapseEnd
End If
Next
Set myRange = ActiveDocument.Range(Start:=pos1, End:=Selection.Start)
myRange.Copy
Set doc = Documents.Add(ActiveDocument.AttachedTemplate.FullName)
Selection.Paste
doc.SaveAs fileName:=name & "_Part" & CStr(i), _
FileFormat:=wdFormatDocumentDefault
doc.Close
i = i + 1
Loop
Set doc = Nothing
Set myRange = Nothing
End Sub
 
A

abqhusker

Hi,

As Jean-Guy explained, the compile error was due to the fact that a line in
my macro was split into two lines. This happened when I copied the macro into
the Reply form. Unfortunately, in my haste I didn't notice and fix this, so
you ended up having the compile error and some frustration. Please acceptmy
apology.

Meanwhile, I fixed that problem and also made a couple of additional
refinements so that the macro will work even better. Note that the original
doc should be a Word2007 doc with the .docx extension. Please try this fixed
version and let us (the newsgroup) know how it works.

Sub SplitDocAndSaveParts()
    Dim myRange As Range
    Dim doc As Document
    Dim name, partName As String
    Dim i, j, k As Integer
    Dim pos1 As Variant
    Dim endFound As Boolean

    endFound = False
    name = ActiveDocument.FullName
    i = 1
    k = InStr(1, name, ".docx")
    name = Left(name, k - 1)
    Selection.HomeKey wdStory
    Do While endFound = False
       pos1 = Selection.Start
       For j = 1 To 10
            ActiveDocument.Bookmarks("\Page").Select
            If ActiveDocument.Bookmarks("\Page").Range.End _
               <> ActiveDocument.Bookmarks("\EndOfDoc").Range.Start Then
               Selection.MoveRight Unit:=wdCharacter, Count:=1
            Else
                endFound = True
                Selection.Collapse Direction:=wdCollapseEnd
            End If
        Next
        Set myRange = ActiveDocument.Range(Start:=pos1, End:=Selection.Start)
        myRange.Copy
        Set doc = Documents.Add(ActiveDocument.AttachedTemplate..FullName)
        Selection.Paste
        doc.SaveAs fileName:=name & "_Part" & CStr(i), _
            FileFormat:=wdFormatDocumentDefault
        doc.Close
        i = i + 1
   Loop
   Set doc = Nothing
   Set myRange = Nothing
End Sub

--
Hope this helps,
Pesach Shelnitz

Jean-Guy Marcil said:
(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
Red means the compiler cannot "understand" a particular line.
In this case, it is because those two lines should be on one line, or, if
you want them on two line, use the line separator thusly:
    doc.SaveAs fileName:=name & "_Part" & CStr(i), _
        FileFormat:=wdFormatDocumentDefault
The line highlighted in yellow is usually the line that gets the compiler to
stumble due to an error. In this case, because the Sub cannot be compiled
(You have a red line above), the compiler highlights the first line because
it could not even start compiling the code, let alone even execute it.

Hi, sorry I didn't get back to you sooner. Right before I left work
Friday, I retried the first code by putting the part that was red on
the same line as instructed by Jean-Guy and it actually worked pretty
good. The only thing was that it didn't maintain the 0.5 margins and
it created an extra blank page at the end of each file but it actually
divided the document at every 10th page like I was wanting it to do so
for the most part, I'm saying it works good. I only got a chance to
run it once so I will try it again when I go into work tomorrow and I
will try with the new code also. I will report back again tomorrow to
tell you how it works but for now, I'm saying it's working and working
pretty good. I really appreciate you all taking the time to help me
with this. You are all awesome and have my utmost respect for you
expertise and willingness to help.

Ed
 
P

Pesach Shelnitz

Hi,

Thank you for your feedback about the macro. I added some code to copy the
margins and page size into each of the new files. This should set your 0.5
margin correctly in the new files.
The problem with the blank line at the end of each new file may be
unavoidable, because the last "carriage return" is needed to preserve the
formatting of the last paragraph.
Here is the revised version of the macro.

Sub SplitDocAndSaveParts()
Dim myRange As Range
Dim doc As Document
Dim name, partName As String
Dim i, j, k As Integer
Dim pSize As WdPaperSize
Dim pWidth, pHeight, hdDist, ftDist As Integer
Dim lMargin, rMargin, tMargin, bMargin As Integer
Dim pos1 As Variant
Dim endFound As Boolean

endFound = False
name = ActiveDocument.FullName
i = 1
k = InStr(1, name, ".docx")
name = Left(name, k - 1)
pSize = ActiveDocument.PageSetup.PaperSize
pHeight = ActiveDocument.PageSetup.PageHeight
pWidth = ActiveDocument.PageSetup.PageWidth
hdDist = ActiveDocument.PageSetup.HeaderDistance
ftDist = ActiveDocument.PageSetup.FooterDistance
lMargin = ActiveDocument.PageSetup.LeftMargin
rMargin = ActiveDocument.PageSetup.RightMargin
tMargin = ActiveDocument.PageSetup.TopMargin
bMargin = ActiveDocument.PageSetup.BottomMargin
Selection.HomeKey wdStory
Do While endFound = False
pos1 = Selection.Start
For j = 1 To 10
ActiveDocument.Bookmarks("\Page").Select
If ActiveDocument.Bookmarks("\Page").Range.End _
<> ActiveDocument.Bookmarks("\EndOfDoc").Range.Start Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Else
endFound = True
Selection.Collapse Direction:=wdCollapseEnd
End If
Next
Set myRange = ActiveDocument.Range(Start:=pos1, End:=Selection.Start)
myRange.Copy
Set doc = Documents.Add(ActiveDocument.AttachedTemplate.FullName)
Selection.Paste
doc.PageSetup.PaperSize = pSize
doc.PageSetup.PageHeight = pHeight
doc.PageSetup.PageWidth = pWidth
doc.PageSetup.HeaderDistance = hdDist
doc.PageSetup.FooterDistance = hdDist
doc.PageSetup.LeftMargin = lMargin
doc.PageSetup.RightMargin = rMargin
doc.PageSetup.TopMargin = tMargin
doc.PageSetup.BottomMargin = bMargin
doc.SaveAs fileName:=name & "_Part" & CStr(i), _
FileFormat:=wdFormatDocumentDefault
doc.Close
i = i + 1
Loop
Set doc = Nothing
Set myRange = Nothing
End Sub
 
G

Greg Maxey

Pesach,

I don't know if it was intentioanal, but many of the variables that you
declared may appear to the OP as one type when they are actually variant
types.

For exmple, in your line:
Dim i, j, k As Integer

Only k is declared as an Integer. i and j are treated as variant types in
the code.

You can observe this with View>Locals Window in the VB Editor.

A better declaration statement might be:

Dim i as Integer, j as Integer, k as Integer
 
A

abqhusker

Hi,

Thank you for your feedback about the macro. I added some code to copy the
margins and page size into each of the new files. This should set your 0.5
margin correctly in the new files.
The problem with the blank line at the end of each new file may be
unavoidable, because the last "carriage return" is needed to preserve the
formatting of the last paragraph.
Here is the revised version of the macro.

Sub SplitDocAndSaveParts()
    Dim myRange As Range
    Dim doc As Document
    Dim name, partName As String
    Dim i, j, k As Integer
    Dim pSize As WdPaperSize
    Dim pWidth, pHeight, hdDist, ftDist As Integer
    Dim lMargin, rMargin, tMargin, bMargin As Integer
    Dim pos1 As Variant
    Dim endFound As Boolean

    endFound = False
    name = ActiveDocument.FullName
    i = 1
    k = InStr(1, name, ".docx")
    name = Left(name, k - 1)
    pSize = ActiveDocument.PageSetup.PaperSize
    pHeight = ActiveDocument.PageSetup.PageHeight
    pWidth = ActiveDocument.PageSetup.PageWidth
    hdDist = ActiveDocument.PageSetup.HeaderDistance
    ftDist = ActiveDocument.PageSetup.FooterDistance
    lMargin = ActiveDocument.PageSetup.LeftMargin
    rMargin = ActiveDocument.PageSetup.RightMargin
    tMargin = ActiveDocument.PageSetup.TopMargin
    bMargin = ActiveDocument.PageSetup.BottomMargin
    Selection.HomeKey wdStory
    Do While endFound = False
       pos1 = Selection.Start
       For j = 1 To 10
            ActiveDocument.Bookmarks("\Page").Select
            If ActiveDocument.Bookmarks("\Page").Range.End _
               <> ActiveDocument.Bookmarks("\EndOfDoc").Range.Start Then
               Selection.MoveRight Unit:=wdCharacter, Count:=1
            Else
                endFound = True
                Selection.Collapse Direction:=wdCollapseEnd
            End If
        Next
        Set myRange = ActiveDocument.Range(Start:=pos1, End:=Selection.Start)
        myRange.Copy
        Set doc = Documents.Add(ActiveDocument.AttachedTemplate..FullName)
        Selection.Paste
        doc.PageSetup.PaperSize = pSize
        doc.PageSetup.PageHeight = pHeight
        doc.PageSetup.PageWidth = pWidth
        doc.PageSetup.HeaderDistance = hdDist
        doc.PageSetup.FooterDistance = hdDist
        doc.PageSetup.LeftMargin = lMargin
        doc.PageSetup.RightMargin = rMargin
        doc.PageSetup.TopMargin = tMargin
        doc.PageSetup.BottomMargin = bMargin
        doc.SaveAs fileName:=name & "_Part" & CStr(i), _
            FileFormat:=wdFormatDocumentDefault
        doc.Close
        i = i + 1
   Loop
   Set doc = Nothing
   Set myRange = Nothing
End Sub

--
Hope this helps,
Pesach Shelnitz






- Show quoted text -

Pesach, hi. I only got one thing to say....You are 100% full-blooded
awesome! I can't thank you enough for taking the time to help me with
this. Both codes worked perfectly and the last one kept the 0.5
margins and divided my document exactly the way it was supposed to
look. I still have the 11th page on each file with one carriage
return on it but I can live with that...still way easier to just
backspace delete that carriage return one time to remove the last
blank page than the way I was having to divide this document
before. I thought the problem might have been the hard page break
at the end of the 10th page and maybe a way to code deleting that last
page break but if the carriage return is necessary, then it wouldn't
matter anyway because the way the document is set up any additional
carriage return will create a another page.

When I first tried the code, I kept getting a "Runtime error 5:
Invalid procedure or call argument" and then in yellow was "name = Left
(name, k - 1)". But then I realized that I was using an old .doc file
to test it and not a .docx file which is what I'm currently using.
Once I used a .docx file, the code ran beautifully. You don't know
how much this helps me...I am elated and ecstatic. I swear, I
searched high and low on Google and could not find anything that
addressed what I wanted to try to do so I thought it must be something
that was next to impossible. I posted to the newsgroup in desperation
thinking there was no solution but here I sit basically having the
solution pretty much the same day (not counting weekend because I
wasn't available to test code) I asked for help. Again, I can't thank
you enough for taking the time to help me with this. I am continually
amazed and astonished at the smart the people on the newsgroups and
their willingness to help clueless people like me. And I know
especially this kind of stuff can't be that easy!

Take care and be safe,

Ed
 
P

Pesach Shelnitz

Hi Greg,

The intention behind my declaration coding style has been to make the types
of my variables clear to readers in the most concise form. I didn't think
that the difference between an Integer and a Variant of subtype Integer is
signifcant for Word macros. However, I respect standards, and since you seem
to want to enforce a standard that is strictly more correct, I'll be happy to
go along with you on this. Meanwhile, this macro works just fine, so I don't
see any point in posting another version, but I will follow your
recommendation in the future.

Thank you for noticing this and posting your feedback.

Pesach
 
P

Pesach Shelnitz

Hi Ed,

I may have a solution to the 11th page issue. After the line

Selection.Paste

try adding the following line:

Selection.Delete Unit:=wdCharacter, Count:=-1
 
A

abqhusker

Hi Ed,

I may have a solution to the 11th page issue. After the line

     Selection.Paste

try adding the following line:

    Selection.Delete Unit:=wdCharacter, Count:=-1

Hi, Pesach. You really are smart with this stuff. I am testing the
code with a 130 page document that I want divided into individual
files of 10 pages each. So in this case I get 13 files. I added the
extra line to the macro and what it did was it made the last file (13)
10 pages but the other 12 files still had 11 pages. Not knowing
anything about writing macros, I fooled around with the number 1 in
that extra line and changed it to a 2 and then I tried it again with
1.5 and they both worked to make all 13 files 10 pages. The only
weird thing is that for some reason the last line of the last page of
the last file (13) ended up with different font but that's an minor
discrepancy as it's basically only the page number and other
insignificant text. It's nothing I'm too concerned about although
it'd be nice to understand why it only happens on that one last file.
I also tried changing the number to 1.2 but only fixed the last file
to 10 pages, all the others were 11, just like using number 1. But
changing it to 1.5 or 2 works but I sure don't understand why. Can
you tell me what exactly is happening with this last line of code you
gave me because it seems to be the right answer to removing that blank
page. Bottom line is, even if we quit tweaking this macro right now,
the macro works as good as anyone could hope and better than I could
wish for. So, again, I want to thank you for all your time and effort
in finding a solution to this dividing the document into files issue.
 
J

Jean-Guy Marcil

Pesach Shelnitz was telling us:
Pesach Shelnitz nous racontait que :
Hi Greg,

The intention behind my declaration coding style has been to make the
types of my variables clear to readers in the most concise form. I
didn't think that the difference between an Integer and a Variant of
subtype Integer is signifcant for Word macros. However, I respect

Depending on the function, methods or properties you use, it can make a
difference, like when handling arrays.
standards, and since you seem to want to enforce a standard that is
strictly more correct, I'll be happy to go along with you on this.
Meanwhile, this macro works just fine, so I don't see any point in
posting another version, but I will follow your recommendation in the
future.

It is just that if you post what looks to be A to a new user, they will
assume that it is A even though it is in fact B.
So, why mislead them when it is easy to do it right? ;-)
 

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