View Full Version : Split doc by number of pages
navic99
02-19-2008, 02:23 PM
I've seen several macros for splitting docs based on splitting based on a particular character, but I'd like to split a very large doc into smaller docs.
 
For example, make a 50,000 page doc into 50 1,000 page docs but not worrying about paragraph placement, formatting or anything like that.
Tinbendr
02-19-2008, 06:34 PM
Snagged this from microsoft.public.word.vba.general
 
 
'_______________________________________ 
Option Explicit 
'_______________________________________ 
Sub SplitBy10() 
 
'Jean-Guy Marcil - Word MVP 
Dim UserRge As Range 
Dim BlockRge As Range 
Dim StartRge As Long 
Dim EndRge As Long 
Dim i As Long 
Dim NewDoc As Document 
Dim CurDocName As String 
Dim CurDocPath As String 
Dim NewDocSuffix1 As String 
Dim NewDocSuffix2 As String 
Dim NewDocName As String 
 
Application.ScreenUpdating = False 
 
Set UserRge = Selection.Range 
Selection.HomeKey wdStory 
 
With ActiveDocument 
.Save 
CurDocName = Left(.Name, Len(.Name) - 4) 
CurDocPath = .Path 
 
For i = 1 To ActiveWindow.ActivePane.Pages.Count / 10 
If i = 1 Then 
StartRge = .Range.Start 
NewDocSuffix1 = "p" & i 
Else 
StartRge = Selection.Start 
NewDocSuffix1 = "p" & ((i * 10) - 9) 
End If 
If i < ActiveWindow.ActivePane.Pages.Count / 10 Then 
Selection.GoTo wdGoToPage, wdGoToAbsolute, (1 * (i * 10)) + 1 
EndRge = Selection.Range.Characters.First.Start 
NewDocSuffix2 = "p" & (i * 10) 
Else 
EndRge = .Range.End 
NewDocSuffix2 = "p" & ActiveWindow.ActivePane.Pages.Count 
End If 
Set BlockRge = .Range(StartRge, EndRge).FormattedText 
Set NewDoc = Documents.Add(Visible:=False) 
NewDocName = CurDocName & "-" & NewDocSuffix1 & "-" & NewDocSuffix2 
& ".doc" 
With NewDoc 
.Range.FormattedText = BlockRge 
.SaveAs CurDocPath & Application.PathSeparator & NewDocName 
.Close 
End With 
Next 
End With 
 
UserRge.Select 
 
Application.ScreenRefresh 
Application.ScreenUpdating = False 
End Sub
navic99
02-20-2008, 06:22 AM
Thanks for finding that.  When I try to run it though, I get this error message:
"Command only available in print layout view"
Tinbendr
02-20-2008, 08:21 AM
Try adding this after the last Dim statement.
 
 
If ActiveWindow.View.SplitSpecial = wdPaneNone Then
ActiveWindow.ActivePane.View.Type = wdPrintView
Else
ActiveWindow.View.Type = wdPrintView
End If
 
 
If you can't get that working, here (http://groups.google.com/group/microsoft.public.word.vba.general/browse_thread/thread/9a059ded172d02fd/2a1f35a0e70e1e9f?hl=en&lnk=gst&q=split+documents+pages#2a1f35a0e70e1e9f) are a couple more.
navic99
02-20-2008, 03:33 PM
That did it...thanks!
fumei
02-21-2008, 11:28 AM
And if you did not use Selection, you would have never got that error.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.