MailMerge Excel到Word单个文件

我正在尝试创build以下内容:

在Excel中,数据被存储以创build具有单个pdf文件中的输出的邮件合并,具有来自数据的文件名“Letter + name”。

个别文件需要用Excel中的“创build字母”button来创build。

我几乎在那里,唯一的问题是,我有个人的PDF文件创build,但所有与每行1相同的数据。

我怎样才能创build具有单独的数据每个文件的单个文件?

Sub RunMailMerge() Dim wdOutputName, wdInputName, PDFFileName As String Dim x As Integer Dim nRows As Integer wdInputName = ThisWorkbook.Path & "\Templates\LetterExample.docx" Const wdFormLetters = 0, wdOpenFormatAuto = 0 Const wdSendToNewDocument = 0, wdDefaultFirstRecord = 1, wdDefaultLastRecord = 3 'This will get you the number of records "-1" accounts for header nRows = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row - 1 ' open the mail merge layout file Dim wdDoc As Object Set wdDoc = GetObject(wdInputName, "Word.document") wdDoc.Application.Visible = False For x = 1 To nRows With wdDoc.MailMerge .MainDocumentType = wdFormLetters .Destination = wdSendToNewDocument .SuppressBlankLines = True With .DataSource .FirstRecord = wdDefaultFirstRecord .LastRecord = wdDefaultLastRecord End With .Execute Pause:=False End With ' show and save output file 'cells(x+1,2)references the first cells starting in row 2 and increasing by 1 row with each loop PDFFileName = ThisWorkbook.Path & "\Letter - " & Sheets(1).Cells(x + 1, 2) & ".pdf" wdDoc.Application.Visible = False wdDoc.ExportAsFixedFormat PDFFileName, 17 ' This line saves a .pdf-version of the mail merge Next x ' cleanup wdDoc.Close SaveChanges:=False Set wdDoc = Nothing MsgBox "Your pdf('s) has now been saved!" End Sub 

尝试在for循环之上移动你的mailmerge,然后遍历for循环中的每个logging集:

 With wdDoc.MailMerge .MainDocumentType = wdFormLetters .Destination = wdSendToNewDocument .SuppressBlankLines = True With .DataSource .FirstRecord = wdDefaultFirstRecord .LastRecord = wdDefaultLastRecord End With .Execute Pause:=False End With For x = 1 To nRows With ActiveDocument.MailMerge.DataSource .ActiveRecord = x If .ActiveRecord > .LastRecord Then Exit For End with '.... rest of your for loop