P
Pesach Shelnitz
Hi,
A week ago, I responded to a thread of a similar name that was started by
(e-mail address removed), I ended up posting several versions of a macro that
accomplished the task almost perfectly, but even the last revision created an
extra blank page at the end of the files created or didn't correctly format
the last paragraph. Splitting a doc at a page break with preservation of all
formatting in the new files created is complicated by the fact that the page
break can be at a point exactly between two paragraphs (the simplest case),
at a point in the middle of a paragraph, at a point within a table, or at
some other tricky point. I worked on the macro a little more, and this
revision can now handle the first two types of page breaks much better, but
it should not be used in cases in which a table spans a page break where the
original file should be split.
This macro may be of interest to anyone who needs to copy formatted portions
of a doc into another doc.
Sub SplitDocByPagesAndSaveParts()
Dim myRange As Range
Dim doc As Document
Dim name, partName As String
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim pSize As WdPaperSize
Dim pWidth As Integer
Dim pHeight As Integer
Dim hdDist As Integer
Dim ftDist As Integer
Dim lMargin As Integer
Dim rMargin As Integer
Dim tMargin As Integer
Dim bMargin As Integer
Dim pos1 As Long
Dim endFound As Boolean
endFound = False
name = ActiveDocument.FullName
i = 1
k = InStr(1, name, ".docx")
If k = 0 Then
MsgBox "The name of the source file must have the .docx extension."
Exit Sub
End If
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
If endFound = False Then
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
Set myRange = ActiveDocument.Range(Start:=pos1,
End:=Selection.Start)
myRange.Copy
Selection.Delete Unit:=wdCharacter, Count:=-1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Else
Set myRange = ActiveDocument.Range(Start:=pos1,
End:=Selection.Start)
myRange.Copy
End If
Set doc = Documents.Add(ActiveDocument.AttachedTemplate.FullName)
Selection.Paste
If endFound = False Then
Selection.Delete Unit:=wdCharacter, Count:=-1
End If
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
Comments are welcome.
Thanks,
Pesach Shelnitz
A week ago, I responded to a thread of a similar name that was started by
(e-mail address removed), I ended up posting several versions of a macro that
accomplished the task almost perfectly, but even the last revision created an
extra blank page at the end of the files created or didn't correctly format
the last paragraph. Splitting a doc at a page break with preservation of all
formatting in the new files created is complicated by the fact that the page
break can be at a point exactly between two paragraphs (the simplest case),
at a point in the middle of a paragraph, at a point within a table, or at
some other tricky point. I worked on the macro a little more, and this
revision can now handle the first two types of page breaks much better, but
it should not be used in cases in which a table spans a page break where the
original file should be split.
This macro may be of interest to anyone who needs to copy formatted portions
of a doc into another doc.
Sub SplitDocByPagesAndSaveParts()
Dim myRange As Range
Dim doc As Document
Dim name, partName As String
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim pSize As WdPaperSize
Dim pWidth As Integer
Dim pHeight As Integer
Dim hdDist As Integer
Dim ftDist As Integer
Dim lMargin As Integer
Dim rMargin As Integer
Dim tMargin As Integer
Dim bMargin As Integer
Dim pos1 As Long
Dim endFound As Boolean
endFound = False
name = ActiveDocument.FullName
i = 1
k = InStr(1, name, ".docx")
If k = 0 Then
MsgBox "The name of the source file must have the .docx extension."
Exit Sub
End If
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
If endFound = False Then
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
Set myRange = ActiveDocument.Range(Start:=pos1,
End:=Selection.Start)
myRange.Copy
Selection.Delete Unit:=wdCharacter, Count:=-1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Else
Set myRange = ActiveDocument.Range(Start:=pos1,
End:=Selection.Start)
myRange.Copy
End If
Set doc = Documents.Add(ActiveDocument.AttachedTemplate.FullName)
Selection.Paste
If endFound = False Then
Selection.Delete Unit:=wdCharacter, Count:=-1
End If
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
Comments are welcome.
Thanks,
Pesach Shelnitz