Hierarchical Mail Merge fails with attached Word Doc

Hi,
I’ve been evaluating Aspose.Words for our organisation by trying to convert an existing document over for mail merging.
I’ve not been able to do a hierarchical mail merge using a DataSet on the attached document for some reason and was hoping you could tell me the cause.
I’ve stripped down the Word doc to a basic version which includes the troublesome spot.
The issue seems to be with the tables under the title “4.1 Selection of appropriate acceptance criteria”. If the tables are deleted or the «TableEnd:Site» merge field is moved before the tables then everything works fine.
Any idea what’s going on with these tables?
Great product by the way, apart from the little snag I’ve encountered.
Cheers, Gavin

Hi,
I’m evaluating Aspose.Words for .NET for our oganisation and have run in to an issue while coding up a proof of concept…
I’m using ExecuteWithRegions passing in a DataSet but I’m having difficulty merging header and footer merge fields with the parent data.
Any suggestions how to achieve this?
What I’m trying to achieve is similar to your ProductCatalogDemo.doc demo code, but I need to merge some fields in a header also.
Perhaps you can modified the attached ProductCatalogDemo.doc to show how this can be achieved?
Thanks in advance for any help!
Cheers, Gavin

Hi Gavin,
Thanks for considering Aspose.
I have merged both of your threads here as I think they relate to the same topic and can both be answered with one post. Thank you for posting your template. I have created a quick sample for you which shows how your template can be merged with hierarchical data. Please see the code below.
In this case it will merge a field in the header using simple mail merge, and will also merge the main document with some nested data. Only the first few fields from some tables are merged just as an example. I have attached the template and the output document produced after running this code as well.

Document doc = new Document(dataDir + "Audit.Form.General.NoRSS.TEST2.doc");
// Create empty dataset used to hold all datatables
DataSet mergeData = new DataSet();
// Create datatables for the main document, make sure the TableName property matches the region in document.
DataTable siteTable = new DataTable();
siteTable.TableName = "Site";
siteTable.Columns.Add("Name");
siteTable.Columns.Add("Location");
mergeData.Tables.Add(siteTable);
// Create child table, including a foreign key column for used for relations.
DataTable activitiesTable = new DataTable();
activitiesTable.TableName = "Activities";
activitiesTable.Columns.Add("ActivityID");
activitiesTable.Columns.Add("PeriodFrom");
// Add the Foreign key
activitiesTable.Columns.Add("SiteName");
mergeData.Tables.Add(activitiesTable);
// Create child table, including a foreign key column for used for relations.
DataTable investigationsTable = new DataTable();
investigationsTable.TableName = "Investigations";
investigationsTable.Columns.Add("ReportDate");
investigationsTable.Columns.Add("ReporterName");
// Add the Foreign key
investigationsTable.Columns.Add("SiteName");
mergeData.Tables.Add(investigationsTable);
// Add some data to the tables
siteTable.Rows.Add(new string[]
{
    "SiteName1",
    "Location1"
});
siteTable.Rows.Add(new string[]
{
    "SiteName2",
    "Location2"
});
// Add child data in activties table for Site1
activitiesTable.Rows.Add(new string[]
{
    "0",
    "Period1",
    "SiteName1"
});
activitiesTable.Rows.Add(new string[]
{
    "1",
    "Period2",
    "SiteName1"
});
// Add child data in activties table for Site2
activitiesTable.Rows.Add(new string[]
{
    "0",
    "Period1",
    "SiteName2"
});
activitiesTable.Rows.Add(new string[]
{
    "1",
    "Period2",
    "SiteName2"
});
// Add child data in investigations table for Site1
investigationsTable.Rows.Add(new string[]
{
    "Date1",
    "Name1",
    "SiteName1"
});
investigationsTable.Rows.Add(new string[]
{
    "Date2",
    "Name2",
    "SiteName1"
});
// Add child data in investigations table for Site2
investigationsTable.Rows.Add(new string[]
{
    "Date1",
    "Name1",
    "SiteName2"
});
investigationsTable.Rows.Add(new string[]
{
    "Date2",
    "Name2",
    "SiteName2"
});
// Set relations between the tables, this enables hierarchical (nested) mail merge to work correctly.
mergeData.Relations.Add(siteTable.Columns["Name"], activitiesTable.Columns["SiteName"]);
mergeData.Relations.Add(siteTable.Columns["Name"], investigationsTable.Columns["SiteName"]);
// Execute simple mail merge to merge the field in the header.
doc.MailMerge.Execute(new string[]
{
    "CompanyName"
}, new string[]
{
    "CompanyNameTest"
});
// Execute mail merge of the main hierarchical data in the document
doc.MailMerge.ExecuteWithRegions(mergeData);
// Save the output to file
doc.Save(dataDir + "Audit.Form.General.NoRSS.TEST2 out.doc");

You can also find detailed information about working with hierarchical (nested) data in mail merge in the nested mail merge documentation here.
If you have any further queries please feel free to ask.
Thanks,

Hi Adam,
Thanks for your reply, unfortunately neither question was really answered and they were both distinctly different problems. Sorry if I wasn’t being clear enough explaining either problem.
I can perhaps surmise from this reply that header / footers can’t be mail merged directly from a DataSet with hierarchical data and I must come up with a work-around? In the example you gave above the workaround is to populate a couple of string arrays and apply it across the whole document using the simple mail merge Execute method. If I know it’s a limitation I can workaround it, so cheers.
As for the second thread you merged. The attached doc was breaking the merge process somehow. I tracked down the problem to some tables (which I emptied of data). When these tables were present in the document and the «TableEnd:Site» mergefield was left after the tables then the whole mail merge would fail. If the «TableEnd:Site» mergefield was moved before the tables then the mail merge would succeed.
The troublesome doc is attached to this thread. Any idea why the tables in it break the Aspose mail merge process? The tables I refer to are under the “4.1 Selection of appropriate acceptance criteria” section of the document. If the tables are deleted then all is good, but I need to keep them for this particular document I’m trying to make work with Aspose.Words.
Please note that the document is a hugely cut down version of a real doc - it’s just been butchered in order to try and discover what was upsetting Aspose.Words.
Cheers again, Gavin

Hi Gavin,
Thanks for this additional information. My apologises, I must of misunderstood what it was you were asking.
You are correct, you can’t have a region inside a header spanning the document body like that, technically because they are separate sections. You can how ever have a separate region contained inside the header/footer itself.
If you are trying to create a header for each repeated region then most likely you will have to include a section break at the end of the region in your template, so the next region begins in a different section and then through code set the header for each section. Please see the code on this page here for details.
Regarding the issue with the merge field being left over, I cannot reproduce it on my side using the sample code I provided above. Is there any other directions you can give as to reproduce it? Also, if you are not using the latest version of Aspose.Words (9.2) then please try using that. You can download it from here.
Thanks,

Hi Adam,
Cheers for the clarification regarding header and footers.
Regarding problem with merging, perhaps it’s partially to do with the data in combination with the word doc that’s causing the failure? I’ll try and set up an example from xml generated from my DataSet when I get a little spare time and post it here. Hopefully tomorrow.
Cheers, Gavin

Hi Gavin,
Yes this is most likely the case. We will be happy to take a look for you when you post your next project.
Thanks,

Hi Adam,

I’ve put together a simple VS 2008 project that re-creates the problem. File attached.

It’s a simple console app which spits out an “output.doc” file. I’ve left the original output.doc file in the zip so you can see what is output when the mailmerge fails.

I’ve also added added a 2nd template doc where I’ve moved the TableEnd tag to before the troublesome section of the template doc. If you uncomment the code that targets this file as the template you’ll see the mailmerge work.

Hopefully you’ll be able to tell me what the issue could be now?

Thanks for looking into this issue for me.

Cheers,
Gavin

Hi Gavin,
Thank you for taking the time creating a simple test project. I have taken a close look into the project and was able to reproduce the issue. However I was only able to reproduce it when I ran the code without a license. When a license was set the merge was completed sucessfully.
It appears as if this behaviour is correct. The document that the merge is being applied to is truncated at a certain point (due to the restriction) before the closing TableEnd field could be reached, and before any merging is done. This would cause the merge to completely fail as the region is not properly closed. Aspose.Words will ignore the entire region since it cannot find the matching TableEnd field thus all fields in the region appear unmerged. Removing the content such as tables, moving the TableEnd merge field before this point or setting a license will cause the merge to be sucessful.
I did notice that the output file that you included with your project still contains the evaluation watermark. If you do not have a license then this is most likely what is causing the problem. You can find details on how to request a 30 day temporary license from here. I apologize for any confusion and inconvenience this may of caused.
Thanks,

Hi Adam,
Thanks for your help.
That’s correct, I haven’t been using a license as we’re just evaluating the product for now. I’ll look in to getting a 30 day trial one when I resume evaluation and see if the issue disapears.
Cheers, Gavin

Hi Adam,
I can confirm everything is working fine with a trial license being loaded.
Cheers, Gavin

Hi Gavin,
That’s great to hear it’s all working now. If you have any further queries please feel free to ask.
Thanks