【问题标题】:Delete line during mail merge if value is 0 or blank (or NULL)如果值为 0 或空白(或 NULL),则在邮件合并期间删除行
【发布时间】:2022-11-12 00:14:19
【问题描述】:

我需要帮助添加我在网上找到的宏,该宏可以自动从邮件合并生成 PDF 的过程。当前的宏进行邮件合并,并作为邮件合并的结果自动生成单个 PDF。见下文:

Sub MailMergeToPdfBasic()                                                        ' Mark the start of the Subroutine (i.e. Macro) and name it "MailMergeToPdf"
' Macro created by Imnoss Ltd
' Please share freely while retaining attribution
' Last Updated 2021-05-03
    Dim masterDoc As Document, singleDoc As Document, lastRecordNum As Long   ' Create variables ("Post-it Notes") for later use
    Set masterDoc = ActiveDocument                                               ' Identify the ActiveDocument (foremost doc when Macro run) as "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                                                                          ' Mark the end of the Subroutine

在我的 Word 文档中,我有值可能为 0 或空白的行项目。每个行项目位于单独的行上。例子:

  • 值 A:1234
  • 值 B:0
  • 值 C:2
  • 值 D:

如果邮件合并字段的值为 0 或空白,是否有办法自动删除行项目。理想的结果是:

  • 值 A:1234
  • 值 C:2

如果它有助于缩小问题范围,我可以在我的 Excel 电子表格中将 0 和空白替换为“NULL”一词:

  • 值 A:1234
  • 值 B:空
  • 值 C:2
  • 值 D:空

但我仍然想要与上述相同的结果:

  • 值 A:1234
  • 值 C:2

我已经在 Google 上搜索了“word 宏删除行”的各种组合,但我并没有真正理解我在网上阅读的内容以及如何修改上面的代码以解决 PDF 生成之前的删除问题。任何帮助,将不胜感激。

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    为此,您不需要任何 VBA - 它都可以通过字段编码在邮件合并本身中处理。

    例如,对于 Value_A:

    {IF{MERGEFIELD Value_A # 0}<> 0 "Value A: {MERGEFIELD Value_A}¶
    "}
    

    并且,对于所有值:

    {IF{MERGEFIELD Value_A # 0}<> 0 "Value A: {MERGEFIELD Value_A}¶
    "}{IF{MERGEFIELD Value_B # 0}<> 0 "Value B: {MERGEFIELD Value_B}¶
    "}{IF{MERGEFIELD Value_C # 0}<> 0 "Value C: {MERGEFIELD Value_C}¶
    "}{IF{MERGEFIELD Value_D # 0}<> 0 "Value D: {MERGEFIELD Value_D}¶
    "}
    

    笔记:上面示例的字段大括号对(即“{ }”)都是在文档本身中创建的,通过 Ctrl-F9(Mac 上的 Cmd-F9,或者,如果您使用的是笔记本电脑,您可能需要使用 Ctrl -Fn-F9);您不能简单地键入它们或从该消息中复制和粘贴它们。通过任何标准的 Word 对话添加它们也不实际。字段结构中表示的空格都是必需的。而不是 ¶ 符号,您应该使用真正的换行符/段落符。

    【讨论】:

    • 谢谢!我将其修改为 { IF {MERGEFIELD VALUE_A } <> "NULL" "Value A: { MERGEFIELD VALUE_A }。它按预期工作,但现在对于 NULL 值,有一个额外的空新行。你如何使用“真实行/分段符”删除额外的新行?
    猜你喜欢
    • 1970-01-01
    • 2015-11-02
    • 2021-07-07
    • 1970-01-01
    • 2017-04-05
    • 2015-10-10
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多