Free Support Forum - aspose.com

Apose.Words MailMerge adding extra page breaks with Directory document type

I created a Mail Merge document in Word 2007 by going to Mailings, Start Mail Merge, and then selecting Directory. If I use that document for a mail merge using Word 2007, all of the records are listed on a single page. If I use MailMerge.Execute, each record is listed on a separate page. If it makes any difference, the merge document has multiple columns.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I think that you can solve this problem by setting section start to Continuous (File/Page setup / Layout tab / select Section start). Also see the following link to learn how to control new pages during mail merge.

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/how-to-control-new-pages-during-mail-merge.html

Also you can attach your template document for testing.

Best regards.

Here is the template document.

I tried adding a continuous section break, but that resulted in a continuous section break and then a page section break after each record.

I noticed that the link that you posted in your reply mentions the ExecuteWithRegions method instead of the Execute method. I'm using the latter. Would that make a difference?

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I think that in this case you should use mail merge with region. I modified your template (see the attachment). And here is sample code:

//Prepare datasource

DataTable myTable = new DataTable("myTable");

myTable.Columns.Add("Name");

myTable.Columns.Add("OrgName");

myTable.Columns.Add("AddressLine1");

myTable.Columns.Add("AddressLine2");

myTable.Columns.Add("City");

myTable.Columns.Add("StateProvinceAbbreviation");

myTable.Columns.Add("PostalCode");

myTable.Columns.Add("Country");

myTable.Columns.Add("PhoneNumber");

myTable.Columns.Add("EmailAddress");

//Add some data

for (int i = 0; i < 20; i++)

{

myTable.Rows.Add(new object[] { "Alexey", "Noskov", "my address", "my address 2", "Auckland", "NZ", "61202", "New Zeland", "1231313", "alexey.noskov@aspose.com" });

}

//Open template

Document doc = new Document(@"Test243\Person+directory+(2007).docx");

//Execute mail merge

doc.MailMerge.ExecuteWithRegions(myTable);

//Save result

doc.Save(@"Test243\out.doc");

Hope this helps.

Best regards.

Hi Alexey. That works great! Our web page has a dropdown list that shows the available mail merge templates. I added code to scan the list of MailMerge field names to see if the TableStart/TableEnd fields are in the template to determine whether we should use the Execute or ExecuteWithRegions method.

Thanks for your help.

Brian.