Calling MailMerge.Execute multiple times for the same doc

Hi,
Can I call MailMerge.Execute for the same doc with different datatables to populate different merge fields ? It seems the current version doesn’t has this flexibility.
It will be great to have this so that instead of writing one huge proc, or a view or a data table on the fly which has columns from various stored procs we can just call mergefields multiple times. Is it possible or do we have to follow one of the methods above ?

Thanks,
Vikas

Hi Vikas,

You can do mail merge several times. The only gotcha is that MailMerge.Execute creates a new destination document, so you will need to do something like:

Document a = Word.Open(fileName);
Document b = a.MailMerge.Execute(tableA);
Document c = b.MailMerge.Execute(tableB);

The reason a new document is created each time is because it can dynamically grow (not in this version, but in v1.2) depending on the number of records.

During mail merge if the fields are found in the document that do not exist in the dataset they are left intact. Another customer today was asking just for the opposite - to remove fields that are left unmerged. I think I will have to make this an option.

Hopefully, in v1.2 you will be able to merge with a dataset that contains multiple related or even unrelated tables. You will be able to mark fields in the document not only by field name, but also by what table they come from. Thus for your scenario you will be able to mail merge only once and still don’t have to create a wide single table.

It would be nice if you email me some concrete samples of your views or stored procedures and where in the document you want to insert the fields. For example, I’m interested in whether fields from different tables are grouped together in the document or mix with fields from other tables.

Hi Roman,
In my case there are fields from many different tables(around 10 or more) and these fields are all spread in the document at random places.
To get around with calling MailMerge only one, I was planning to create a view which has a join to these 10 or more tables and will return one data table.
If dataset with multiple tables can be fed to MailMerge.Execute that that will be great since we can then write stored procs or functions which can be reused in other projects within the same business domain. Will this be available in beta, so that we can architect the system accordingly?
Also does it matter if the MergeFields are spread in the document. My assumption was that the Execute function matches the field data(in the datatable) to the MergeFields in the doc as per the name … so basically we will have to be carefull about returning the datatable with the field names.

If you need samples let me know.

Thanks,
Vikas

Hi Vikas,

A sample would be useful. I will be able to advise you on the strategy best aligned with the way Aspose.Word will evolve.

As a guideline for you consider two examples:

Example 1: “Not sure what this document is about”.
For this type of document you will probably be better off merging columns from two tables in a view, especially if there is one to one match between the rows.

Some text
field from table a
some text
field from table b
some text
field from table a
some text
field from table b
and so on.


Example 2: "Invoice or catalog"
For this document you will need to insert mail merge fields and also special marking that tells Aspose.Word where table begins and ends (so far it looks like mail merge fields will be used for this purpose too). Then you will create and populate a dataset in an appropriate way and call MailMerge just once.

Invoice header that uses fields from table A (say Order)
Invoice header that uses fields from table B (say Customer joined to Order via relationship in the DataSet)
Invoice details from table C (say OrderDetails)
Invoice footer from table A (Order)

Another document that conforms to the above sample:

Catalog name from table A
Product category from table B
Product details from table C
Product picture from binary data or filename.

-----
Beta released tomorrow will have support for headers, footers and images only. Support for dataset mail merging will be released around 23rd Feb for the first time.

Just as an idea for your current situation.

1. Leave all your views and stored procedures as they are without joining into one huge view in the database.
2. In the code create a DataTable and programmatically create columns and records that you need for the merge by pulling data from all of the other views and stored procedures.
3. Feed that DataTable into Aspose.Word.

In this way you isolate all this “hack” code in one place. In some ways it is similar to creating a single huge view in the database, but it is better if some of the data you need is returned by stored procedures. I think you cannot include results of stored procedures in the database view, but with this approach you can.

Later, if your problem maps well into Aspose.Word support for DataSet you can just throw away the “hack” code if you want.

Hi Roman,
I did gave a thought with the data table as opposed to view but it becomes really huge for large document. That’s the reason I want to stick to the dataset way or the earlier method you mentioned:

Document a = Word.Open(fileName);
Document b = a.MailMerge.Execute(tableA);
Document c = b.MailMerge.Execute(tableB);

Is this feature available in beta?


Thanks,
Vikas.




This approach with multiple calls to MailMerge works in current Aspose.Word 1.1. You can try it out right now. You need to name the fields carefully so they are unique between different tables. Let me know if you are happy with the outcome once its wotking.