split document based on Page number input by user using VBA
I have a document of several pages. I need to split it based on number of page supplied, by user. so following macro doing great when I run it by F8
key(Step by step) from VBA editor, However when I try to run this macro directly, it stuck in second loop of For x to TP/P
I guess. Don't know what is cause of the problem. I have to deal with the Headers and footers after that too.
Sub Pages_for_Yoho()
'Pages Update 3.0 By M.B.A.
Application.ScreenUpdating = False
Dim oDoc As Document
Dim oNewDoc As Document
Dim Crng As Range
Dim XrngSplit As Range
Dim DocName, Start_Name As String
Dim TP, P, GoP As Integer
Set oDoc = ActiveDocument
Start_Name = InputBox("Write first part of File name?", "First Part", "") & " "
TP = oDoc.Content.Information(wdNumberOfPagesInDocument)
ST: P = Int(InputBox("How many pages per file?", "Number of pages", ""))
If P = "" Then GoTo ST
If IsNumeric(P) = False Then GoTo ST
If P >= TP Then GoTo ST
Set Crng = oDoc.Range
'Crng.Select
For x = 1 To TP / P
GoP = GoP + P
Set XrngSplit = oDoc.Range
XrngSplit.GoTo wdGoToPage, wdGoToAbsolute, GoP + 1
Crng.End = XrngSplit.Start
Crng.Copy ' Error line which is caused by previous lines
Set oNewDoc = Documents.Add(Visible:=False)
oNewDoc.Sections.PageSetup.DifferentFirstPageHeaderFooter = True
oNewDoc.Range.Paste
DocName = Trim(Start_Name & GoP - P & " - " & GoP & ".docx")
oNewDoc.SaveAs FileName:=oDoc.Path & Application.PathSeparator & DocName, AddToRecentFiles:=False
'== Progress Bar =='
Application.StatusBar = "Separating Pages " & GoP - P + 1 & " to " & GoP & " ------> " & Int((GoP / (TP)) * 100) & "% Completed..."
'=================='
oNewDoc.Close
Crng.Collapse 0
Next
If TP Mod P > 0 Then
'Set XrngSplit = ActiveDocument.GoTo(wdGoToPage, wdGoToAbsolute, TP)
'Set XrngSplit = XrngSplit.GoTo(What:=wdGoToBookmark, Name:="\page")
Crng.End = oDoc.Range.End
Crng.Copy
Set oNewDoc = Documents.Add(Visible:=False)
oNewDoc.Sections.PageSetup.DifferentFirstPageHeaderFooter = True
oNewDoc.Range.Paste
DocName = Trim(Start_Name & GoP + 1 & " - " & TP & ".docx")
'Debug.Print DocName
oNewDoc.SaveAs FileName:=oDoc.Path & Application.PathSeparator & DocName, AddToRecentFiles:=False
'== Progress Bar =='
Application.StatusBar = "Separating Pages " & GoP + 1 & " - " & TP & " ------> " & Int((TP / TP) * 100) & "% Completed..."
'=================='
oNewDoc.Close
Crng.Collapse 0
End If
Beep
Application.ScreenUpdating = True
End Sub
Error: 4605
Please point out problem in my code?
Set XrngSplit = oDoc.Range
At this point both XrngSplit
and Crng
have the same start and end points
XrngSplit.GoTo wdGoToPage, wdGoToAbsolute, GoP + 1
Because you have not used GoTo
correctly the above statement does not change XrngSplit
so, at the next line, XrngSplit.Start
is the same as Crng.Start
which results in Crng
being an empty range, i.e. it has zero length because Crng.End = Crng.Start
. This causes the error when you try to copy Crng
.
To fix this you need
Set XrngSplit = oDoc.GoTo(wdGoToPage, wdGoToAbsolute, GoP + 1)
Crng.End = XrngSplit.Start