从Excel VBA合并pdf文件

问题描述

我正在尝试使用VBA将合并后的邮件从Excel邮件发送到单个pdf文件

这是一个小的测试示例:

enter image description here

单词文档“ Testdoc.docx”看起来像这样:

enter image description here

这是VBA代码

Sub test()
    Dim wdOutputName,wdInputName,PDFFileName As String
    Dim x As Long
    Dim nRows As Long
    
    wdInputName = ThisWorkbook.Path & "\Testdoc.docx"
    Const wdFormletters = 0,wdOpenFormatAuto = 0
    Const wdSendToNewDocument = 0,wdDefaultFirstRecord = 1,wdDefaultLastRecord = 3
    nRows = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row - 1

    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
        
        PDFFileName = ThisWorkbook.Path & "\Letter " & Sheets(1).Cells(x + 1,1) & ".pdf"
        wdDoc.Application.Visible = False
        wdDoc.ExportAsFixedFormat PDFFileName,17
        Next x

    wdDoc.Close SaveChanges:=False
    Set wdDoc = nothing
    MsgBox "Succes"
End Sub

问题:

它制作3个单独的pdf文件并分别正确命名:“ Letter name1.pdf”,“ Letter name2.pdf”和“ Letter name3.pdf”。 但是,它不能正确合并合并字段:所有文档如下:

enter image description here

<<Name>>应该分别用“ name1”,“ name2”和“ name3”代替,但这不是事实。

任何想法如何使其起作用?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)