Thread: [Solved] Merge into Separate Docs
View Single Post
 
Old 02-28-2022, 10:13 PM
chrnon chrnon is offline Windows 10 Office 2013
Novice
 
Join Date: Feb 2022
Posts: 1
chrnon is on a distinguished road
Default Merge into Separate Docs

Hello,
I have been trying to use the macro listed in "tips and tricks" to merge into individual documents. It has worked for me in the past but this time, it will not work.
I have renamed "MailMergeToDoc". When I go back to the main document, finish and merge, edit individual documents.... it doesn't do anything. I am not sure what I am doing wrong or why it will not initiate.
If anyone can help me out, I would really appreciate!

My code:

Code:
Sub MailMergeToDoc()
' Sourced from: https://www.msofficeforums.com/mail-...ps-tricks.html
Application.ScreenUpdating = False
Dim StrFolder As String, StrName As String, MainDoc As Document, i As Long, j As Long
Const StrNoChr As String = """*./\:?|"
Set MainDoc = ActiveDocument
With MainDoc
  StrFolder = .Path & "\"
  With .MailMerge
    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True
    On Error Resume Next
    For i = 1 To .DataSource.RecordCount
      With .DataSource
        .FirstRecord = i
        .LastRecord = i
        .ActiveRecord = i
        If Trim(.DataFields("Last_Name")) = "" Then Exit For
        'StrFolder = .DataFields("Folder") & "\"
        StrName = .DataFields("Folder_Name") & "_" & .DataFields("Last_Name")
      End With
      On Error GoTo NextRecord
      .Execute Pause:=False
      For j = 1 To Len(StrNoChr)
        StrName = Replace(StrName, Mid(StrNoChr, j, 1), "_")
      Next
      StrName = Trim(StrName)
      With ActiveDocument
        'Add the name to the footer
        '.Sections(1).Footers(wdHeaderFooterPrimary).Range.InsertBefore StrName
        .SaveAs FileName:=StrFolder & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
        ' and/or:
        .SaveAs FileName:=StrFolder & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
        .Close SaveChanges:=False
      End With
NextRecord:
    Next i
  End With
End With
Application.ScreenUpdating = True
End Sub
Reply With Quote