View Single Post
 
Old 01-04-2021, 02:23 AM
ales.jamsek ales.jamsek is offline Windows 10 Office 2016
Novice
 
Join Date: Jan 2021
Posts: 4
ales.jamsek is on a distinguished road
Default Merge group by key and split to individual pdf files

Hello everyone!

With the help from few posts from these forums I have managed to almost completely solve my problem which is: I want to create certificates for users that attended different classes during the year and where each user's certificate would list all the classes he attended. I have data stored in attached xlsx file. Fields to group and merge data are in my main document (merge_main.docx).

If I just run normal mail-merge in word, merged document gets created just fine, each page has certificate for one user and all classes he attended are listed.

But then I added to the main document following macro to separate merged document to individual pdf files:

Code:
Sub Merge_To_Individual_Files()
'
' Merges one record at a time to the "potrdila" sub-folder

Application.ScreenUpdating = False
Dim StrFolder As String, StrName As String, MainDoc As Document, i As Long, j As Long
Set MainDoc = ActiveDocument
With MainDoc
  StrFolder = .Path & "\potrdila\"
  With CreateObject("Scripting.FileSystemObject")
    If Not .FolderExists(StrFolder) Then .CreateFolder StrFolder
  End With
  For i = 1 To .MailMerge.DataSource.RecordCount
    With .MailMerge
      .Destination = wdSendToNewDocument
      .SuppressBlankLines = True
      With .DataSource
        .FirstRecord = i
        .LastRecord = i
        .ActiveRecord = i
        StrName = .DataFields("User")
      End With
      .Execute Pause:=False
    End With
    With ActiveDocument
      .SaveAs2 FileName:=StrFolder & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
      .Close SaveChanges:=False
    End With
  Next i
End With
Application.ScreenUpdating = True

End Sub
Pdf files are created fine, but the problem is, that for each user just the first class he attended is listed. Where did I go wrong?

Thanks.
Attached Files
File Type: docx merge_main.docx (29.8 KB, 7 views)
File Type: xlsx merge_data.xlsx (9.7 KB, 5 views)
Reply With Quote