title: How do I split my one document into many at page breaks in VBA?
body: <p>I am new to VBA. I got this macro from online and it has worked for me before, but now I am getting a runtime error from it. </p>
  
  <p>The macro is supposed to take a mail merged document I have, and split it into individual documents for each recipient. </p>
  
  <p>The runtime error 5487 is pointing me to the line </p>
  
  <pre><code>" .SaveAs fileName:=StrTxt &amp;...". 
  </code></pre>
  
  <p>I have tried to save it as a different file format, and have gone through the other posts on StackOverflow where others have the same error, but I am still getting the error message. </p>
  
  <p>My code is:</p>
  
  <pre><code>Sub SplitMergedDocument()
    ' Source: http://msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
    Const StrNoChr As String = """*./\:?|"
    Dim i As Long, j As Long, k As Long, StrTxt As String
    Dim Rng As Range, Doc As Document, HdFt As HeaderFooter
    Application.ScreenUpdating = False
    j = InputBox("How many Section breaks are there per record?", "Split By Sections ", 1)
    With ActiveDocument
        For i = 1 To .Sections.Count - 1 Step j ' Process each Section
            With .Sections(i)
                Set Rng = .Range.Paragraphs(1).Range ' Get 1st paragraph
                With Rng
                    .MoveEnd wdCharacter, -1 'range to exclude final paragraph break
                    StrTxt = .Text
                    For k = 1 To Len(StrNoChr)
                        StrTxt = Replace(StrTxt, Mid(StrNoChr, k, 1), "_")
                    Next
                End With
                ' Construct destination file path &amp; name
                StrTxt = ActiveDocument.Path &amp; Application.PathSeparator &amp; StrTxt
                Set Rng = .Range ' Get whole Section
                With Rng
                    If j &gt; 1 Then .MoveEnd wdSection, j - 1
                    .MoveEnd wdCharacter, -1 'Contract range to exclude Section break
                    .Copy ' Copy range
                End With
            End With
            ' Create output document
            Set Doc = Documents.Add(Template:=ActiveDocument.AttachedTemplate.FullName _
                , Visible:=False)
            With Doc
                ' Paste contents into output document, preserving formatting
                .Range.PasteAndFormat (wdFormatOriginalFormatting)
                ' Delete trailing paragraph breaks &amp; page breaks at end
                While .Characters.Last.Previous = vbCr Or .Characters.Last.Previous = Chr(12)
                    .Characters.Last.Previous = vbNullString
                Wend
                For Each HdFt In Rng.Sections(j).Headers ' Replicate headers &amp; footers
                    .Sections(j).Headers(HdFt.Index).Range.FormattedText = HdFt.Range.FormattedText
                Next
                For Each HdFt In Rng.Sections(j).Footers
                    .Sections(j).Footers(HdFt.Index).Range.FormattedText = HdFt.Range.FormattedText
                Next
                ' Save &amp; close output document
                .SaveAs FileName:=StrTxt &amp; ".docx", FileFormat:=wdFormatXMLDocument _
                      , AddToRecentFiles:=False
                .Close SaveChanges:=False
            End With
        Next
    End With
    Set Rng = Nothing: Set Doc = Nothing
    Application.ScreenUpdating = True
  End Sub
  </code></pre>
  
  <p>Thank you!</p>