代码之家  ›  专栏  ›  技术社区  ›  JY1993

如何修复邮件合并的运行时错误?

  •  0
  • JY1993  · 技术社区  · 2 年前

    我使用下面的宏来保存邮件合并中的单个word文档和pdf。宏运行并保存第一个字母的两个文件,但后来我遇到了这个错误,我不知道如何修复它。

    宏: Sub-MailMergeToPdfBasic()'标记子例程(即宏)的开始,并将其命名为“MailMergeToPdf” 'Imnoss有限公司创建的宏 “请在保留归因的同时自由分享 '上次更新时间:2021-05-03 Dim masterDoc As Document,singleDoc As Document,lastRecordNum As Long'创建变量(“便利贴”)以备日后使用 设置masterDoc=ActiveDocument'将ActiveDocument(宏运行时最前面的文档)标识为“masterDoc”

    masterDoc.MailMerge.DataSource.ActiveRecord = wdLastRecord                   ' jump to the last active record (active = ticked in edit recipients)
    lastRecordNum = masterDoc.MailMerge.DataSource.ActiveRecord                  ' retrieve the record number of the last active record so we know when to stop
    
    masterDoc.MailMerge.DataSource.ActiveRecord = wdFirstRecord                  ' jump to the first active record (active = ticked in edit recipients)
    Do While lastRecordNum > 0                                                   ' create a loop, lastRecordNum is used to end the loop by setting to zero (see below)
        masterDoc.MailMerge.Destination = wdSendToNewDocument                    ' Identify that we are creating a word docx (and no e.g. an email)
        masterDoc.MailMerge.DataSource.FirstRecord = masterDoc.MailMerge.DataSource.ActiveRecord              ' Limit the selection to just one document by setting the start ...
        masterDoc.MailMerge.DataSource.LastRecord = masterDoc.MailMerge.DataSource.ActiveRecord               ' ... and end points to the active record
        masterDoc.MailMerge.Execute False                                        ' run the MailMerge based on the above settings (i.e. for one record)
        Set singleDoc = ActiveDocument                                           ' Identify the ActiveDocument (foremost doc after running the MailMerge) as "singleDoc"
        singleDoc.SaveAs2 _
            FileName:=masterDoc.MailMerge.DataSource.DataFields("DocFolderPath").Value & Application.PathSeparator & _
                masterDoc.MailMerge.DataSource.DataFields("DocFileName").Value & ".docx", _
            FileFormat:=wdFormatXMLDocument                                      ' Save "singleDoc" as a word docx with the details provided in the DocFolderPath and DocFileName fields in the MailMerge data
        singleDoc.ExportAsFixedFormat _
            OutputFileName:=masterDoc.MailMerge.DataSource.DataFields("PdfFolderPath").Value & Application.PathSeparator & _
                masterDoc.MailMerge.DataSource.DataFields("PdfFileName").Value & ".pdf", _
            ExportFormat:=wdExportFormatPDF                                      ' Export "singleDoc" as a PDF with the details provided in the PdfFolderPath and PdfFileName fields in the MailMerge data
        singleDoc.Close False                                                    ' Close "singleDoc", the variable "singleDoc" can now be used for the next record when created
        If masterDoc.MailMerge.DataSource.ActiveRecord >= lastRecordNum Then     ' test if we have just created a document for the last record
            lastRecordNum = 0                                                    ' if so we set lastRecordNum to zero to indicate that the loop should end
        Else
            masterDoc.MailMerge.DataSource.ActiveRecord = wdNextRecord           ' otherwise go to the next active record
        End If
    
    Loop                                                                         ' loop back to the Do start
    

    End Sub标记子程序的结束

    当我调试时,我会突出显示这部分代码。

    singleDoc。另存为2_ FileName:=masterDoc。邮件合并。DataSource。数据字段(“DocFolderPath”)。价值&应用路径分隔符&_ masterDoc。邮件合并。DataSource。数据字段(“DocFileName”)。价值&“.docx”_ FileFormat:=wdFormatXMLDocument'将“singleDoc”保存为单词docx,并在MailMerge数据的DocFolderPath和DocFileName字段中提供详细信息 singleDoc。导出为固定格式_

    我已经尝试将上面的第一行更新为SaveAs,但超过第一个字母后仍然无法工作。

    0 回复  |  直到 2 年前
    推荐文章