title: VBA Splitting Merged Documents; works in Windows but not in MacOS?
body: <p>Thank you in advance for anyone reading this and able to help :)</p>
  <p>I've adapted a macro for splitting a merged document in Word. It works great on Windows and performs as expected. However, on MacOS, it crashes and pops up with &quot;Run-time error 4198: Command Failed.&quot; Part of the difficulty is that I don't actually even use a Mac; I sent the macro to a colleague to test and that's when I got the error. This had made troubleshooting even more difficult... Whenever that colleague clicked 'Debug' the error centered around the following line. Full code below:</p>
  <p><code>.SaveAs FileName:=StrTxt &amp; &quot;.docx&quot;, FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False</code></p>
  <p>Full code:</p>
  <pre><code>
  ' Edited from: https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
  Application.ScreenUpdating = False
  Dim i As Long, j As Long, k As Long, StrTxt As String
  Dim rng As Range, Doc As Document, HdFt As HeaderFooter
  
  Const StrNoChr As String = &quot;&quot;&quot;*./\:?|&quot;
  
  j = InputBox(&quot;How many Section breaks are there per record?&quot;, &quot;Split By Sections&quot;, 1)
  
  With ActiveDocument
  ' Process each Section
  For i = 1 To .Sections.Count - 1 Step j
  With .Sections(i)
  
  '*****
  ' Get the 1st paragraph
  Set rng = .Range.Paragraphs(1).Range
  With rng
  ' Contract the range to exclude the final paragraph break
  .MoveEnd wdCharacter, -1
  StrTxt = .Text
  For k = 1 To Len(StrNoChr)
  StrTxt = Replace(StrTxt, Mid(StrNoChr, k, 1), &quot;_&quot;)
  Next
  End With
  
  ' Construct the destination file path &amp; name
  StrTxt = ActiveDocument.Path &amp; &quot;\&quot; &amp; StrTxt
  '*****
  ' Get the whole Section
  Set rng = .Range
  With rng
  If j &gt; 1 Then .MoveEnd wdSection, j - 1
  'Contract the range to exclude the Section break
  .MoveEnd wdCharacter, -1
  ' Copy the range
  .Copy
  End With
  End With
  
  ' Create the output document
  Set Doc = Documents.Add(Template:=ActiveDocument.AttachedTemplate.FullName, Visible:=False)
  
  With Doc
  ' Paste contents into the output document, preserving the formatting
  .Range.PasteAndFormat (wdFormatOriginalFormatting)
  ' Delete trailing paragraph breaks &amp; page breaks at the end
  While .Characters.Last.Previous = vbCr Or .Characters.Last.Previous = Chr(12)
  .Characters.Last.Previous = vbNullString
  Wend
  
  ' Replicate the headers &amp; footers
  For Each HdFt In rng.Sections(j).Headers
  .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 the output document
  
  .SaveAs FileName:=StrTxt &amp; &quot;.docx&quot;, FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
  
  ' and/or:
  
  ' .SaveAs FileName:=StrTxt &amp; &quot;.pdf&quot;, FileFormat:=wdFormatPDF, AddToRecentFiles:=False
  .Close SaveChanges:=False
  End With
  
  Next
  
  End With
  
  Set rng = Nothing: Set Doc = Nothing
  Application.ScreenUpdating = True
  
  End Sub```
  </code></pre>