通过VB.NET将Word表中的数据进行邮件合并

通过VB.NET将Word表中的数据进行邮件合并,vb.net,interop,mailmerge,Vb.net,Interop,Mailmerge,上面的代码基本上在合并期间显示word文档中不同页面中的所有productName 请帮助我如何将数据放入表中。我必须为我的ProductName、AccountNo、OutBalance、AccountName等编写多个代码。我的问题是我不知道如何将它们放入表中。如果您也对第三方库感兴趣,可以尝试使用我们的组件 下面是一个示例VB.NET代码(通过从代码中创建模板文档并从文件中加载它)如何执行此操作: 什么样的桌子?word文档中的表或数据库表或word文档中的什么?表。我在这里要做的是用ma

上面的代码基本上在合并期间显示word文档中不同页面中的所有productName


请帮助我如何将数据放入表中。我必须为我的ProductName、AccountNo、OutBalance、AccountName等编写多个代码。我的问题是我不知道如何将它们放入表中。

如果您也对第三方库感兴趣,可以尝试使用我们的组件

下面是一个示例VB.NET代码(通过从代码中创建模板文档并从文件中加载它)如何执行此操作:


什么样的桌子?word文档中的表或数据库表或word文档中的什么?表。我在这里要做的是用mailmerge中的数据填充表。我可以显示数据,但我无法将它们逐个单元格放入表中。
wrdMergeFields.Add(wrdSelection.Range, "ProductName")
' Use the component in free mode.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")

' Define DataTable with two columns: 'Name' and 'Surname', and fill it with some data.
' You don't have to do this if you already have a DataTable instance.
Dim dataTable = New DataTable("People")
dataTable.Columns.Add(New DataColumn("Name", GetType(String)))
dataTable.Columns.Add(New DataColumn("Surname", GetType(String)))
dataTable.Rows.Add("John", "Doe")
dataTable.Rows.Add("Fred", "Nurk")
dataTable.Rows.Add("Hans", "Meier")
dataTable.Rows.Add("Ivan", "Horvat")

' Create and save a template document. 
' You don't have to do this if you already have a template document.
' This code is only provided as a reference how template document should look like.
Dim document = New DocumentModel()
document.Sections.Add(
    New Section(document,
        New Table(document,
            New TableRow(document,
                New TableCell(document,
                    New Paragraph(document, "Name")),
                New TableCell(document,
                    New Paragraph(document, "Surname"))),
            New TableRow(document,
                New TableCell(document,
                    New Paragraph(document,
                        New Field(document, FieldType.MergeField, "RangeStart:People"),
                        New Field(document, FieldType.MergeField, "Name"))),
                New TableCell(document,
                    New Paragraph(document,
                        New Field(document, FieldType.MergeField, "Surname"),
                        New Field(document, FieldType.MergeField, "RangeEnd:People")))))))
document.Save("TemplateDocument.docx", SaveOptions.DocxDefault)

' Load a template document.
document = DocumentModel.Load("TemplateDocument.docx", LoadOptions.DocxDefault)

' Mail merge template document with DataTable.
' Important: DataTable.TableName and RangeStart/RangeEnd merge field names must match.
document.MailMerge.ExecuteRange(dataTable)

' Save the mail merged document.
document.Save("Document.docx", SaveOptions.DocxDefault)

' Open the documents with MS Word.
Process.Start("TemplateDocument.docx")
Process.Start("Document.docx")