Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

Param
maxRows
maxRows

Specifies the maximum number of rows to import from the data source. To import the maximum rows available, use the constant WordTemplate.ALL_ROWS.{param}

Param
transpose
transpose

If transpose is set to true, the array is treated as Object[column][row]. If transpose is set to false, the array is treated as Object[row][column].

...

Remarks

You can call SetMailMerge once for each instance of WordTemplate. If you are using the Excel Word 2003 binary template file type (.xlsdoc/.xltdot), you can call SetMailMerge or SetRepeatBlock, but not both.

Additionally, only the page content is repeated for each row, not the entire 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:

  • Put the cursor at the top of the document
  • Go to Page Layout and open the paragraph formatting dialog
  • On the Line and Page Breaks tab,  select "Page Break Before"

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

Introducedin
8.2
8.2

SetMailMerge will now work with headers and footers. A section break is required instead of a page break if each header or footer will be different.

Example
Code Block
csharp
csharp
titleC#

          ...
          //--- A 2-D jagged array of values
          //--- This is a "transposed" array with columns in the first
          //--- dimension and rows in the second
          object[][] data = new object[][]{
               new string[]{"Knoxville", "Boston",
               "Washington", "Seattle",
               "Chicago", "New York",
               "Atlanta", "Los Angeles",
               "Houston"},
               new string[]{"Tennessee", "Massachusetts",
               "DC", "Washington",
               "Illinois", "New York",
               "Georgia", "California",
               "Texas"}
               };

          //--- Names array, elements correspond to merge field names
          string[] names = new string[] { "City", "State" };
          WordTemplate wt = new WordTemplate();
          wt.Open(Server.MapPath("template/MergeFieldTest.doc"));

          //--- Set the mail merge
          //--- The data source is the 2-D jagged data array
          //--- MaxRows is set to ALL_ROWS, which allows all rows to be imported
          //--- Transpose is set to true to handle the transposed array
          wt.SetMailMerge(data, names, WordTemplate.ALL_ROWS, true);
          wt.Process();
          wt.Save(Page.Response, "output.doc", false);
        
Code Block
vb.net
vb.net
titlevb.net

          '--- A 2-D jagged array of values
          '--- This is a "transposed" array with columns in the first
          '--- dimension and rows in the second
          Dim data()() As Object = { _
               New Object() {"Knoxville", "Boston", _
              "Washington", "Seattle", _
               "Chicago", "New York", _
               "Atlanta", "Los Angeles", _
               "Houston"}, _
               New Object() {"Tennessee", "Massachusetts", _
               "DC", "Washington", _
               "Illinois", "New York", _
               "Georgia", "California", _
               "Texas"} _
               }

          '--- names array, elements correspond to merge field names
          Dim names() As String = {"City", "State"}
          Dim wt As New WordTemplate()
          wt.Open(Server.MapPath("template/MergeFieldTest.doc"))

          '--- Set the mail merge
          '--- The data source is the jagged data array
          '--- MaxRows is set to ALL_ROWS, which allows all rows to be imported
          '--- Transpose is set to true to handle the transposed array
          wt.SetMailMerge(data, names, WordTemplate.ALL_ROWS, True)
          wt.Process()
          wt.Save(Page.Response, "output.doc", False)