How to use regions in mail merge/smart marker in ms word template?

yes, now working.Thanks.
I have 1 more query. Please find the attachment.

Is this template is correct?
1.I want to repeat the name of the customer.
2. I want to repeat the department and professorsDoubt.docx (14.6 KB)
.

@Rakesh_M Template syntax depends on your data source. It looks like you need nested mail merge feature. Please see our documentation for more information:
https://docs.aspose.com/words/java/nested-mail-merge-with-regions/

Ok, Thanks,i’ll look into that.
1Query:
i’m executing the example application
( https://reference.aspose.com/words/java/com.aspose.words/imailmergedatasource/ )
this is forking fine with

doc.getMailMerge().execute(customersDataSource);

but if I change to executeWithRegions it is not working. What can i do to make it work?

doc.getMailMerge().executeWithRegions(customersDataSource);

@Rakesh_M Make sure in your implementation of IMailMergeDataSource, the method getTableName returns correct table name. For example if in your implementation getTableName looks like this:

public String getTableName() {
    return "Customer";
}

The region should look like this:
{{ #foreach Customer }}.......{{ /foreach Customer }}

No, It is not working . Please find the attachment for code base and the output.

doubt.zip (61.3 KB)

Could you please provide your suggestions.

adding few issues to existing sample application.

  1. doc.getMailMerge().executeWithRegions(customersDataSource);
    if i run with executeWithRegions() method, it is not displaying the complete output for regions. It is displaying only one value. Please find the the output below specified.

«FullName»
«Address»
• xyz

  1. doc.getMailMerge().executeWithRegions(customersDataSource);
    if i run with execute() method, it is not displaying the
    regions data. nd it is displaying like this
    «TableStart:list»«listofDoc»«TableEnd:list»

Could you please confirm , is this one expected ?
Please find the out put attachments.

executeWithRegions_output.pdf (43.3 KB)
execute_output.pdf (49.3 KB)

@Rakesh_M In your code you are execution only simple mail merge as a result only field outside the regions are filled. To fill both fields outside the region and the fields inside region, you should execute simple mail merge and mail merge with regions.

How do i do that both?
Can i execute both these doc.getMailMerge().executeWithRegions(customersDataSource);
doc.getMailMerge().execute(customersDataSource);

Could you also please check my query before i asked.

@Rakesh_M Yes, you can execute both execute and executeWithRegions methods. But in your case I think you should use nested mail merge with regions. In this case your template should look like this:

 {{ #foreach Customers }}
      .......
      {{ #foreach list }}.......{{ /foreach list }}
 {{ /foreach Customers }}

In this case root data source is Customers and for each customer record in IMailMergeDataSource the getChildDataSource should return data source for list.

  1. For Customers object , just i’m holding Fullname and Address that’s it.

there is no dependency between customer and listofDoc.

i need to run those Fullname and address are independent smart markers and listofDoc is a separate one.

{{FullName}}

{{Address}}

• {{ #foreach list }}{{listofDoc}}{{ /foreach list }}

listofDoc is need to be repeated ,like i want to use regions for this and Fullname and Address are independent.

Could you please suggest now?

in simple words , i want to run few smart markers with out repeating (no region is required)
and few are required with regions(need to repeat).

how do i execute this?
please advise.

Hi,
I’m attaching my simple application. Please find the attachments.
issue is:
Only region data is displaying.
Issue With executeWithRegions.zip (56.6 KB)

Could you please check my application and please suggest the solution.
I’m attaching the output file as well.

@Rakesh_M In your code only mail merge with regions is executed. As I mentioned earlier to fill both fields outside the region and the fields inside region, you should execute simple mail merge and mail merge with regions.
Simple mail merge to fill fields outside the region.
Mail merge with regions to fill the region with data.

Hi,
Still not working, please find the attachment for out put files.
1.
TemplateToPdf2 .pdf file is output for below specified order.

doc.getMailMerge().executeWithRegions(ds);
doc.getMailMerge().execute(ds);

TemplateToPdf2.pdf (44.1 KB)
TemplateToPdf1.pdf (45.6 KB)
file is output for below specified order.

doc.getMailMerge().execute(ds);
doc.getMailMerge().executeWithRegions(ds);

Could you pls advise for the solution?TemplateToPdf2.pdf (44.1 KB)

@Rakesh_M In your case you should use separate data sources for region and for fields outside the region, since, as you have mentioned, region data and data outside the region are not related. Foer example see the following code:

DataTable dt = new DataTable("list");
dt.getColumns().add("Number");
for (int i = 0; i < 10; i++)
    dt.getRows().add("item" + i);

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().setUseNonMergeFields(true);
// Execute simple mail merge to fill fields outside the region.
// For demonstration purposes I use arrays as data source
doc.getMailMerge().execute(new String[] { "cName", "addressLine", "acount" }, new String[] { "James Bond", "MI5", "007" });
// Execute mail merge with regions to fill region with data.
doc.getMailMerge().executeWithRegions(dt);
doc.save("C:\\Temp\\out.pdf");

in.docx (13.6 KB)
out.pdf (25.6 KB)

Thanks, Yes, this example is working.
But what was the issue in my application?

And do you provide any sample for custom data source example having both non region data and region data.

@Rakesh_M When you use the same data source to execute mail merge twice, you should reset position of record in data source. After executing mail merge first time moveNext method returns false. So when you try to execute mail merge second time using the same data source without resetting position, the data source is considered as empty.

Hi,
im not getting the proper output.
i need the the output like this below specified for the above application.
Could you please suggest, what are the changes need to do in my code.
need code changes helps me.

cName
Account
address1

  • docName1
  • docName2

Can i use for 2 methods for this requirement?app.zip (13.8 KB)

@Rakesh_M To get the expected output your should use two data source.
The first one for Account, address1 and cName, which will be used with MailMerge.execute method and fill fields outside the region.
The second data source for documents list, which is used for filling region with data and is used with MailMerge.executeWithRegions method.

ok, thanks. I’ll try.

1 Like