title: How to split a mail merge and save files with a merge field as the name
body: <p>I have a bunch of mail merge templates setup, when I merge the documents I want to split the results into separate files each one with a name based on the merge field “FileNumber”.</p>
  
  <p>The code I have currently is:</p>
  
  <pre><code>Sub splitter()
  ' Based on a macro by Doug Robbins to save each letter created by a mailmerge as a separate file.
  ' With help from http://www.productivitytalk.com/forums/topic/3927-visual-basic-question-for-merge-fields/
  
  Dim i As Integer
  Dim Source As Document
  Dim Target As Document
  Dim Letter As Range
  Dim oField As Field
  Dim FileNum As String
  
  Set Source = ActiveDocument
  
  For i = 1 To Source.Sections.Count
      Set Letter = Source.Sections(i).Range
      Letter.End = Letter.End - 1
          For Each oField In Letter.Fields
          If oField.Type = wdFieldMergeField Then
              If InStr(oField.Code.Text, "FileNumber") &gt; 0 Then
              'get the result and store it the FileNum variable
              FileNum = oField.Result
              End If
          End If
          Next oField
      Set Target = Documents.Add
      Target.Range = Letter
      Target.SaveAs FileName:="C:\Temp\Letter" &amp; FileNum
      Target.Close
      Next i
  End Sub
  </code></pre>
  
  <p><strong>The problem is if I “Merge to new document” then the “FileNumber” field no longer exists so it can’t pick that up but if I just go to “Preview Results” and run the macro it only saves the currently previewed record and not the rest of the letters.</strong></p>
  
  <p>I’m assuming I need to change the code to something like </p>
  
  <pre><code>For i = 1 To Source.MergedRecord.Count
      Set Letter = Source.MergedRecord(i).Range
  </code></pre>
  
  <p>but I can't work out the correct syntax.</p>
  
  <p>I am aware of <a href="http://www.gmayor.com/individual_merge_letters.htm" rel="nofollow">http://www.gmayor.com/individual_merge_letters.htm</a> but I don't want the dialog boxes I just want a one click button.</p>