PDA

View Full Version : Split on page break



Cabrill
01-22-2014, 04:30 PM
Hello, and thank you for taking the time to read this post. I'm trying to use VBA in Word 2010 to split up a document with both section breaks and page breaks scattered at unpredictable points. How can I easily grab each group of sections between page breaks to copy into new documents? This is the code I've been working with:



Private Sub BreakOnPageBreak(myDoc As Document)
Dim MyNumPages As Integer
Dim newDoc As Document
Dim mySection As Section

MyNumPages = 0
For Each mySection In ActiveDocument.Sections
MyNumPages = MyNumPages + 1
mySection.Range.Copy
Set newDoc = Documents.Add
Selection.Collapse Direction:=wdCollapseStart
newDoc.Range.PasteAndFormat (wdFormatOriginalFormatting)
newDoc.SaveAs FileName:=SourceName & "_" & MyNumPages & ".doc", _
AddToRecentFiles:=False
newDoc.Close
Next mySection
myDoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub


The problem, as you may have guessed, is that it copies each individual section to a new document and not the whole sections that exist between page breaks. I've tried experiementing with



If mySection.Range.PageSetup.SectionStart = wdSectionNewPage
pageRange.Start = mySection.Range.Start
ElseIf ...?


However, I couldn't figure out a method to determine if the section I'm looking at is the LAST section before a page break so I could set the end of my pageRange before copying. Would anyone be so kind as to help me iterate through the content between page breaks to copy it out? Thank you in advance ever so much for your appreciated assistance!

fumei
01-22-2014, 07:47 PM
Are the page breaks you are talking about ALL hard breaks? If they are not you have a problem. Word does not really have a page object. Sort of, but it is a bit vague.

I am a little confused. So you do not want any part of the last section that is BEFORE the last page break? Just whole sections between page breaks. So very small sections, less than one page long. I.e. most likely a Continuous Section.

Cabrill
01-22-2014, 09:19 PM
Thank you very much for your response, fumei!

The page breaks I'm referring to ARE all hard breaks, so not a problem there. I do need the last section before the last page break, I just don't know of a way to confirm that I'm on the last section before the page ends so that I can set the end of my 'pageRange' to be the end of that section.

So, in psuedo-code I envision it would be something like this:


myPageRange.Start = firstSectionOnPage.Start
myPageRange.End = lastSectionOnPage.End

That way I get all the sections between hard page breaks, right? Then I just copy the content of myPageRange to a new document. If I'm on the first section in the document, or if the section I just left was the last section of the prior page then I know I'm on the first section of a new page, but how do I know that I'm on the last section of the current page? Thanks again!

fumei
01-23-2014, 03:29 PM
Sorry, but I am even more confused now. What on earth do you mean by "the section I just left"? Left? That sounds like some sort of movement. Please describe precisely a sample, say three pages, and what sections are involved. It may also help if you state what exactly you are trying to do - are you trying to get sections, or pages. They are quite different beasts.

Can you post a sample document? I am not following your pseudo code, even why you are trying to set a range object like that. If you have two different sections on the same page, then they are continuous sections.