PDA

View Full Version : Split two at a time



qewldude
07-01-2010, 09:24 AM
Hey, I want to split 2 pages at a time i.e copy the 2 pages from a document and then put those two pages in another document.

Here is a code but this only does 1 page at a time, but i need to copy two at a time. I have been killing myself for the past 3 days for it to work trying out different possibilities. Could you please help me out? Thanks!!



Option Explicit 'This goes in the Declarations section of your code module.
'Hopefully it is already there because you have ticked the 'Require Variable Declaration' _
checkbox. (Tools/Options, Editor tab.)


Sub SplitIntoPages()
Dim docMultiple As Document
Dim docSingle As Document
Dim rngPage As Range
Dim iCurrentPage As Integer
Dim iPageCount As Integer
Dim strNewFileName As String

Application.ScreenUpdating = False 'Makes the code run faster and reduces screen _
flicker a bit.
Set docMultiple = ActiveDocument 'Work on the active document _
(the one currently containing the Selection)
Set rngPage = docMultiple.Range 'instantiate the range object
iCurrentPage = 1
'get the document's page count
iPageCount = docMultiple.Content.ComputeStatistics(wdStatisticPages)
Do Until iCurrentPage > iPageCount
If iCurrentPage = iPageCount Then
rngPage.End = ActiveDocument.Range.End 'last page (there won't be a next page)
Else
'Find the beginning of the next page
'Must use the Selection object. The Range.Goto method will not work on a page
Selection.GoTo wdGoToPage, wdGoToAbsolute, iCurrentPage + 1
'Set the end of the range to the point between the pages
rngPage.End = Selection.Start
End If
rngPage.Copy 'copy the page into the Windows clipboard
Set docSingle = Documents.Add 'create a new document
docSingle.Range.Paste 'paste the clipboard contents to the new document
'remove any manual page break to prevent a second blank
docSingle.Range.Find.Execute Findtext:="^m", ReplaceWith:=""
'build a new sequentially-numbered file name based on the original multi-paged file name and path
strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc")
docSingle.SaveAs strNewFileName 'save the new single-paged document
iCurrentPage = iCurrentPage + 1 'move to the next page
docSingle.Close 'close the new document
rngPage.Collapse wdCollapseEnd 'go to the next page
Loop 'go to the top of the do loop
Application.ScreenUpdating = True 'restore the screen updating

'Destroy the objects.
'(Not really necessary. VB does it automatically when the objects go out _
of scope, but they like it in this forum.)
Set docMultiple = Nothing
Set docSingle = Nothing
Set rngPage = Nothing
End Sub

Tinbendr
07-01-2010, 06:08 PM
Welcome to VBA Express!


Selection.GoTo wdGoToPage, wdGoToAbsolute, iCurrentPage + 1
'.....
iCurrentPage = iCurrentPage + 1 'move to the next page
Change the +1 to +2 in the two locations.

See the similar request at this message. (http://www.vbaexpress.com/forum/showthread.php?t=32675&highlight=pages)

fumei
07-07-2010, 12:11 PM
As an alternative...

Sub ExtractTwoPages()
Dim r As Range
Dim ThisDoc As Document
Dim ThatDoc As Document
Dim j As Long
Dim pageCount As Long
Dim var
Dim Found As Long

Set ThisDoc = ActiveDocument
pageCount = ThisDoc.Range.ComputeStatistics(wdStatisticPages)
Set r = ThisDoc.Range( _
Start:=0, End:=0)
For var = 1 To pageCount / 2
With r
Found = .MoveEndUntil(Cset:=Chr(12))
If Found <> 0 Then
.MoveEnd Unit:=wdCharacter, Count:=2
Found = .MoveEndUntil(Cset:=Chr(12))
If Found = 0 Then
.End = ThisDoc.Range.End
End If
End If
End With
Set ThatDoc = Documents.Add
With ThatDoc
.Range = r
.SaveAs FileName:="c:\Temp\PageChunk_" & j & ".doc"
.Close
End With
j = j + 1
Set ThatDoc = Nothing
With r
.Collapse 0
.MoveStart Unit:=wdCharacter, Count:=1
End With
Next
End Sub


1. does not use Selection - as you do not need to.

2. you do not state anything about the page breaks. Are they manual breaks, or Word generated breaks?

3. you do not state if this is for the entire document, or not. The code above creates separate documents for every two pages starting at page 1.

4. the Found code is a test to see if the MoveEndUntil returns a value > 0. If it has, then it found a page break. If it returns 0 then no page break was found, thus it has reached the last page. In which case the range .End becomes the document range .End.

5. the last movement of the range parameters is to ensure the page break used to extract the previous two pages is NOT included in the next extracted two pages.

fumei
07-07-2010, 12:16 PM
Notice also there is NO copying and pasting required. The next document objects simply take the latest range as its range.

Voila!

Of a six page document, you get:

PageChunk_0.doc = page 1 and 2
PageChunk_1.doc = page 3 and 4
PageChunk_2.doc = page 5 and 6

Note that in a five page document, you get:

PageChunk_0.doc = page 1 and 2
PageChunk_1.doc = page 3 and 4

Page 5 is NOT extracted. Why? You did not ask that would be.

It would be VERY easy (a single line) to change it so the last (non-two page chunk) would also be extracted.

HINT: it is an added ELSE instruction.