Free Support Forum - aspose.com

MailMerge.ExecuteWithRegions and headers and footer?

Hi
1)I have a template with some merge field in header and footer:
1.Header:
<> <> <>
2.Main body:
<> <> … (other fields)

<>

3.Footer:
<> <> <>

2)I am using my own MyDataSource:IMailMergeDataSource;

3)PROBLEM: when I call MailMerge.ExecuteWithRegions(myDS)
Only header is merged. The rest of the document is unchanged.

4)What I tried:
I called MailMerge.ExecuteWithRegions(myDS) 3 times, creating a new instance of MyDataSource every time:
MyDataSource myDS = new MyDataSource(data);
doc.MailMerge.ExecuteWithRegions(ds); //this merges header
MyDataSource myDS = new MyDataSource(data);

doc.MailMerge.ExecuteWithRegions(ds); //this merges main body

MyDataSource myDS = new MyDataSource(data);

doc.MailMerge.ExecuteWithRegions(ds); ////this merges footer

This approach seems to work but is this the right way to do this.
As far as i can see I need to call ExecuteWithRegions now as many times as there are <> mergefileds in my document as a single call seems to only do one of them?
I am sure this is not the way it is designed to behave by Aspose.

5)I wonder if this only happens with custom DataSources?
Or may be I did not implement one of the methods correctly in MyDataSource?

I would expect IMailMergeDataSource to have a method to reset it to original state which should be called by Aspose after all rows from the root DataTable (Quote in my case) are merged in the header and a new <> is encountered again?

I mean Aspose certainly allows opening a child table multiple times in the document as this works just fine (note 2 pairs of TableStart…TableEnd for table called FirstChild!):
<>
<>
<>
<>

<>

<>

So I kind of expected that the same would work for the root table, but it does not seem to (using MyDataSource - may work for System.Data.DataSet as Datasource?):

<>



<>

<>


<>

Note: I tried with DataSet and it does not handle situation either :frowning: !

Thanks!
William











Hi William,


Thanks for your inquiry.

Yes, this is the expected behaviour, mostly because of the design of how the mail merge engine works. The solution to achieve merging of duplicate regions is to use IMailMergeDataSourceRoot or a DataSet (just add your one table to the dataset). Please see below for an example. In your case “MyDataSource” is the current IMailMergeDataSource you are using. You would now instead merge using MyDataSourceRoot. The reason why this works is explained further below.

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:blue;mso-no-proof:yes”>public<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> class MyDataSourceRoot
: IMailMergeDataSourceRoot<o:p></o:p>

{

public IMailMergeDataSource GetDataSource(string tableName)

{

// This will be called for each duplicate region in the document.

return new MyDataSource();

}

}


When you pass data to mail merge engine which represents only one table (e.g DataTable, IMailMergeDataSource, DataView) the mail merge engine will only invoke merging of this table one time (the first instance if there are duplicate regions with the same name).

However if you pass a data source which represents multiple tables (IMailMergeDataSourceRoot) then the mail merge engine leaves merging of all tables up to this object. In this situation the root object will be called for each region found in the document (and also for each duplicate regions of the same name). As usual it should automatically return a new instance of IMailMergeDataSource to merge each of the duplicate regions.

Therefore you can either use IMailMergeDataSourceRoot or a DataSet to merge duplicate regions in the document but using IMailMergeDataSource or DataTable on it's own will not.

There have been multiple requests for this sort of thing, I have logged a request for the default behaviour in this case to be changed so that duplicate regions will be merged like in your situation. We will inform you of any developments.

Thanks,

Thanks very much Adam,
this worked.

The issues you have found earlier (filed as WORDSNET-5290) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(2)