title: split document based on Page number input by user using VBA
body: <p>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 <code>F8</code> key(Step by step) from VBA editor, However when I try to run this macro directly, it stuck in second loop of <code>For x to TP/P</code> I guess. Don't know what is cause of the problem. I have to deal with the Headers and footers after that too.</p>
  <pre><code>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(&quot;Write first part of File name?&quot;, &quot;First Part&quot;, &quot;&quot;) &amp; &quot; &quot;
      
      TP = oDoc.Content.Information(wdNumberOfPagesInDocument)
  
  ST:     P = Int(InputBox(&quot;How many pages per file?&quot;, &quot;Number of pages&quot;, &quot;&quot;))
      
      If P = &quot;&quot; Then GoTo ST
      If IsNumeric(P) = False Then GoTo ST
      If P &gt;= 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 &amp; GoP - P &amp; &quot; - &quot; &amp; GoP &amp; &quot;.docx&quot;)
              
          oNewDoc.SaveAs FileName:=oDoc.Path &amp; Application.PathSeparator &amp; DocName, AddToRecentFiles:=False
          
          '== Progress Bar =='
          Application.StatusBar = &quot;Separating Pages &quot; &amp; GoP - P + 1 &amp; &quot; to &quot; &amp; GoP &amp; &quot; ------&gt;   &quot; &amp; Int((GoP / (TP)) * 100) &amp; &quot;% Completed...&quot;
          '=================='
          
          oNewDoc.Close
          
  
          Crng.Collapse 0
      
      Next
      
      
      If TP Mod P &gt; 0 Then
      
          'Set XrngSplit = ActiveDocument.GoTo(wdGoToPage, wdGoToAbsolute, TP)
          
          'Set XrngSplit = XrngSplit.GoTo(What:=wdGoToBookmark, Name:=&quot;\page&quot;)
          
          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 &amp; GoP + 1 &amp; &quot; - &quot; &amp; TP &amp; &quot;.docx&quot;)
          
      'Debug.Print DocName
      
          oNewDoc.SaveAs FileName:=oDoc.Path &amp; Application.PathSeparator &amp; DocName, AddToRecentFiles:=False
          
          '== Progress Bar =='
          Application.StatusBar = &quot;Separating Pages &quot; &amp; GoP + 1 &amp; &quot; - &quot; &amp; TP &amp; &quot; ------&gt;   &quot; &amp; Int((TP / TP) * 100) &amp; &quot;% Completed...&quot;
          '=================='
          
          oNewDoc.Close
          
  
          Crng.Collapse 0
          
      End If
         
      Beep
      Application.ScreenUpdating = True
      
  End Sub
  </code></pre>
  <p><em>Error: 4605</em></p>
  <p><a href="https://i.stack.imgur.com/qUNFB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qUNFB.png" alt="enter image description here" /></a></p>
  <p><strong>Please point out problem in my code?</strong></p>