0
wrdMergeFields.Add(wrdSelection.Range, "ProductName")

上記のコードは基本的に、マージ中に Word Document のさまざまなページのすべての productName を表示します。

テーブル内にデータを配置する方法を教えてください。ProductName、AccountNo、OutBalance、AccountName などのコードを複数記述する必要があります。ここでの問題は、それらをテーブルに入れる方法がわからないことです。

4

1 に答える 1

0

問題を解決するためにサードパーティのライブラリにも興味がある場合は、GemBox.Documentコンポーネントを試すことができます。

これを行う方法のサンプル VB.NET コードを次に示します (コードからテンプレート ドキュメントを作成し、それをファイルからロードします)。

' 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")
于 2012-07-02T08:15:12.400 に答える