Message-ID: <158069671.7753.1711620163218.JavaMail.web05$@web05> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_7752_1069215281.1711620163202" ------=_Part_7752_1069215281.1711620163202 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html WordTemplate.SetMailMerge(System.Data.DataTable)

WordTemplate.SetMailMerge(System.Data.DataTable)

Description

=20

Sets the MailMerge's data source to a two-dimensional (rectangular) arra= y of objects. A MailMerge by default treats the page content as a repeat bl= ock, repeating the page content for each row unless NEXT fields are used. I= f the WordTemplate.E= nableNEXTFields property is set to true, the NEXT field can also be use= d to indicate that the next row of data should be inserted instead of the c= urrent row at the next occurence of the merge fields.

=20
C#
=20
 public void SetMailMerge(System.Data.DataTable dt)
=20
=20
vb.net
=20
Public Sub SetMailMerge(ByVal dt As System.Data.DataTable)
=20
=20

Para= meters

=20
dt
An A= DO.NET DataTable to use as a data source.=20

Exce= ptions

=20
ArgumentNullException
=20 Save will t= hrow this exception if=20 null (C#) or=20 Nothing (VB.NET) is passed to the method.=20
ArgumentException
=20

Remarks=

=20

You can call SetMailMerge once for each instance of WordTemplate. If you are using the Word= 2003 binary template file type (.doc/.dot), you can call SetMailMerge or S= etRepeatBlock, but not both.

=20

Additionally, only the page content is repeated for each row, not the en= tire page itself. If you wish to have the page itself repeat for each row, = you will need to remember to place a page break at the bottom of the page. = Alternatively, you can create a hidden page break at the top of the page as= follows:

=20 =20

Merge Fields for using the SetMailMerge method must not specify a data s= ource =E2=80=93 the data source is implied, and using a data source name wi= ll cause WordTemplate to throw an error. Valid merge field formats for use = with SetMailMerge include field names («fieldname») and field ordinals («#1»).

=20

Introduced in build 8.2

=20

SetM= ailMerge will now work with headers and footers. A section break is req= uired instead of a page break if each header or footer will be different.=20

Exampl= es

=20
C#
=20

          //--- Create connection string for Excel file data
          string connString =3D
               "Provider=3DMicrosoft.Jet.OLEDB.4.0;" +
               "Data Source=3D" + Server.MapPath("data/MailM=
ergeData.xls") + ";" +
               "Extended Properties=3DExcel 8.0;";

          //--- Use OleDb to get all data from Sheet1 of the MailMergeData.=
xls spreadsheet
          OleDbConnection conn =3D new OleDbConnection(connString);
          conn.Open();
          OleDbCommand cmd =3D new OleDbCommand("SELECT * FROM [Sheet1=
$]", conn);
          OleDbDataAdapter adapter =3D new OleDbDataAdapter();
          adapter.SelectCommand =3D cmd;

          //--- Dump the data into a DataTable
          DataTable dt =3D new DataTable();
          adapter.Fill(dt);
          WordTemplate wt =3D new WordTemplate();
          wt.Open(Server.MapPath("template/MergeFieldTest.doc"));

          //--- Set the mail merge
          //--- The data source is the DataTable
          wt.SetMailMerge(dt);
          wt.Process();
          wt.Save(Page.Response, "output.doc", false);
          ...
        
=20
=20
vb.net
=20

          '--- Create connection string for Excel file data
          Dim connString As String =3D _
               "Provider=3DMicrosoft.Jet.OLEDB.4.0;" & _
               "Data Source=3D" & Server.MapPath("data/M=
ailMergeData.xls") & ";" & _
               "Extended Properties=3DExcel 8.0;"

          '--- Use OleDb to get all data from Sheet1 of the MailMergeData.x=
ls spreadsheet
          Dim conn As New OleDbConnection(connString)
          conn.Open()
          Dim cmd As New OleDbCommand("SELECT * FROM [Sheet1$]", =
conn)
          Dim adapter As New OleDbDataAdapter()
          adapter.SelectCommand =3D cmd

          '--- Dump the data into a DataTable
          Dim dt As New DataTable()
          adapter.Fill(dt)
          Dim wt As New WordTemplate()
          wt.Open(Server.MapPath("template\MergeFieldTest.doc"))

          '--- Set the mail merge
          '--- The data source is the DataTable
          wt.SetMailMerge(dt)
          wt.Process()
          wt.Save(Page.Response, "output.doc", False)
        
=20
------=_Part_7752_1069215281.1711620163202--