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

WordTemplate.SetMailMerge(System.Data.DataSet, Int32)

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.DataSet ds, int maxRows)
=20
=20
vb.net
=20
Public Sub SetMailMerge(ByVal ds As System.Data.DataSet, ByVal maxRows As I=
nteger)
=20
=20

= Parameters

=20
ds
= An ADO.NET DataSet to use as a data source.=20
max= Rows
Specifies the maximum number of rows to import from the data sour= ce. To import the maximum rows available, use the constant=20 WordTemplat= e.ALL_ROWS.=20

= Exceptions

=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

Rem= arks

=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

Ex= amples

=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 DataSet
          DataSet ds =3D new DataSet();
          adapter.Fill(ds);
          WordTemplate wt =3D new WordTemplate();
          wt.Open(Server.MapPath("template/MergeFieldTest.doc"));

          //--- Set the mail merge
          //--- The data source is the DataSet
          //--- MaxRows is set to ALL_ROWS, which allows all rows to be imp=
orted
          wt.SetMailMerge(ds, WordTemplate.ALL_ROWS);
          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;" amp; _
               "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 DataSet
          Dim ds As New DataSet()
          adapter.Fill(ds)
          Dim wt As New WordTemplate()
          wt.Open(Server.MapPath("template\MergeFieldTest.doc"))

          '--- Set the mail merge
          '--- The data source is the DataSet
          '--- MaxRows is set to ALL_ROWS, which allows all rows to be impo=
rted
          wt.SetMailMerge(ds, WordTemplate.ALL_ROWS)
          wt.Process()
          wt.Save(Page.Response, "output.doc", False)
        
=20
------=_Part_8262_913294021.1711639076615--