VBA Table 在合并的信件中

VBA Table in Merged Letter

我有一个 Excel Sheet 有很多客户数据。所有客户都有共同的数据(地址、姓名等),我将这些数据实现为简单的合并字段。一些客户有多个数据集,应在合并信件的末尾添加为 Table。 要从我的 excel Sheet 中查找数据,我已经想出了以下代码。 noInt 是客户的数量,而 noData 是不同数据集的数量(所有客户加在一起,一些倍数)。 exWb 是我的数据来自的 excel 工作簿,我想在 table 中显示的数据位于第 5 至 9 列。

For i = 2 To noInt
    
    For k = 2 To noData
        
        If exWb.Sheets("Table1").Cells(k, 1) = exWb.Sheets("Table2").Cells(i, 1) Then
            For j = 5 To 9
            

插入 Table exWb.Sheets("Table1").Cells(k, j)

            Next j
        End If
    Next k
Next i

现在我的问题:

  1. 如何将此数据插入新创建的 table 占位符“insert_table_here”之后?

  2. 如何确保邮件合并系列中的每封信都只有客户的数据,这封信是关于包含在这个table中的?

    为了解决这个问题,我已经考虑过是否有一个函数可以提供当前的“邮件合并编号”。在这种情况下,我可以将字段 (MailMergeNumber, 1) 与 (k,1) 进行比较,以仅显示包含当前客户的结果。

示例使其更易于理解:

尊敬的 A 先生,

...

A 先生购买了

Table 件商品

-文档结束-

亲爱的 B 先生,

...

B 先生购买了

Table 件商品

-文档结束-

等等...

如果您从模板创建 Word 文档(这通常是我发现的最简单的方法),您可以使用 header 添加 table 到模板文档您需要的行,以及数据的 1 个空白行。然后,在填充基本的合并字段之后,您可以循环遍历当前的客户字段,边走边向 Word table 添加新行。像这样:

Dim exWs as Excel.Worksheet
Dim CurrentCustomerFirstCell as Excel.Range
Dim CurrentCustomerActiveCell as Excel.Range
Dim EmpRowOffset as integer
Dim wdDoc as Word.Document
Dim wdTable as Word.Table, wdCell as Word.Cell

' set up your existing references, including (I assume) to the Word document you're updating

set exWs = exWb.Sheets("Table1")

' initialize row for current employee
CurrentCustomerFirstCell = exWs.Cells(2,1)

do while CurrentCustomerFirstCell.Row <= noData ' consider renaming noData to somthing like "numberOfRows"
    ' populate basic mergefields
    wdDoc.Fields(1).Result.Text = CurrentCustomerFirstCell.Value
    ' etc.

    ' populate table in Word document
    set wdTable = wdDoc.Tables(1)

    EmpRowOffset = 0
    set CurrentCustomerActiveCell = CurrentCustomerFirstCell.Offset(Rowoffset:=EmpRowOffset)

    set wdTable = wdDoc.Tables(1)

    do while CurrentCustomerActiveCell.Value = CurrentCustomerFirstCell.Value
        ' this code would update the first "data" row in the existing Word table
        ' to the 6th column of the active employee row
        set wdCell = wdTable.Cell(Row:=2 + EmpRowOffset, Column:=1)
        wdCell.Range.Text = _
                CurrentCustomerActiveCell.Offset(columnoffset:=5).Value
        wdTable.Rows.Add

        EmpRowOffset = EmpRowOffset + 1
        set CurrentCustomerActiveCell = CurrentCustomerFirstCell.Offset(RowOffset:=EmpRowOffset)
    Loop

    ' now that we're finished processing the employee, update CurrentCustomerFirstCell
    set CurrentCustomerFirstCell = CurrentCustomerActiveCell
loop

您可以为此使用 Word 的 Catalogue/Directory 邮件合并工具(术语取决于 Word 版本)。要了解如何使用 Word 支持的任何邮件合并数据源执行此操作,请查看我的 Microsoft Word Catalogue/Directory 邮件合并教程,网址为:

http://www.msofficeforums.com/mail-merge/38721-microsoft-word-catalogue-directory-mailmerge-tutorial.html

或:

http://www.gmayor.com/Zips/Catalogue%20Mailmerge.zip

本教程涵盖了从列表创建到插入和计算多记录 table 中字母值的所有内容。在尝试使用其中包含的邮​​件合并文档之前,请务必阅读本教程。

根据您要实现的目标,字段编码可能会很复杂。然而,由于教程文档包含所有示例的工作字段代码,大部分的艰苦工作已经为您完成 - 您应该能够做的只是 copy/paste 将相关字段代码放入您自己的邮件合并中主文档,substitute/insert 您自己的字段名称并调整格式以获得您想要的结果。有关一些工作示例,请参阅帖子的附件:

http://www.msofficeforums.com/mail-merge/9180-mail-merge-duplicate-names-but-different-dollar.html#post23345

http://www.msofficeforums.com/mail-merge/11436-access-word-creating-list-multiple-records.html#post30327

另一种选择是在普通的“信件”邮件合并主文档中使用数据库字段,并使用宏来驱动该过程。可以在以下位置找到此方法的概述:

http://answers.microsoft.com/en-us/office/forum/office_2010-word/many-to-one-email-merge-using-tables/8bce1798-fbe8-41f9-a121-1996c14dca5d

相反,如果您使用的是关系数据库,或者 Excel 带有单独 table 且每个分组条件只有一个实例的工作簿,则正常 ' 中的 DATABASE 字段letter' mailmerge 主文档可以在不需要宏的情况下使用。可以在以下位置找到此方法的概述:

https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-mso_winother-mso_2010/mail-merge-to-a-word-table-on-a-single-page/4edb4654-27e0-47d2-bd5f-8642e46fa103

有关工作示例,请参阅:

http://www.msofficeforums.com/mail-merge/37844-mail-merge-using-one-excel-file-multiple.html

或者,您可能想尝试多对一邮件合并插件之一,来自:

http://www.gmayor.com/ManyToOne.htm 的格雷厄姆市长;或

道格·罗宾斯在 https://onedrive.live.com/?cid=5AEDCB43615E886B&id=5AEDCB43615E886B!566