Free Support Forum - aspose.com

MergeField in the Header/Footer

Hi

I have an issue. I would like to use put a merge field in the header of a document with Regions.

I have to put the field «TableStart:Base» somewhere and Words won't allow me to put it in the header. If I do I get a:

"Mail merge region 'Base' is badly formed. TableStart and TableEnd should be in the same section, same table row or same table cell."

and if a put the «TableStart:Base» below the header then all other fields including the child table get populated perfectly, but the merge field in the header gets left out.

I have attached two templates with both alternatives. Grateful for any pointers as to how this can be achieved,

!Rob

Hello

Thanks for your inquiry.

You should follow these simple rules when marking a region:

· TableStart and TableEnd fields must be inside the same section in the document.

· If used inside a table, TableStart and TableEnd must be inside the same row in the table.

· Mail merge regions can be nested inside each other.

· Mail merge regions should be well formed (there is always a pair of matching TableStart and TableEnd with the same table name).

· Duplicate regions with the same name are allowed. However to successfully merge duplicated regions, the mail merge process must be executed twice.

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/mail-merge-with-regions-explained.html

I can suggect you a very simple way to work this problem around. You csn try using MailMerge.ExecuteWithRegions for the mail part of a document and simple MailMerge.Execute for a document header.

Best regards,

Hi

FYI, I will get back to this thread once the below thread has been sorted out.
Since I did not quite understand how to:
"try using MailMerge.ExecuteWithRegions for the mail part of a document and simple MailMerge.Execute for a document header. "

I simply did both:

doc.MailMerge.Execute(dt);
doc.MailMerge.ExecuteWithRegions(ds);

The result was confusing since I am still not on top of the abovementioned thread.

!Rob

Hi again

Now that the other issue with this thread has been sorted out, time has come for me to look into this, your suggestion:


"I can suggect you a very simple way to work this problem around. You csn try using MailMerge.ExecuteWithRegions for the mail part of a document and simple MailMerge.Execute for a document header. "

My approach above doing both:

doc.MailMerge.Execute(dt);
doc.MailMerge.ExecuteWithRegions(ds);

Was clearly a dud, it produced a resulting document that had multiplied by itself. That is perhaps no great wonder, I just gave it a go.

So my questions are:

How do I use MailMerge.ExecuteWithRegions for the mail part of a document?

How do I use MailMerge.Execute for the document header?

Regards,

!Rob

Hello

Thanks for your inquiry. Please try using the following code:

Document doc = new Document("C:\\Temp\\in.docx");

doc.MailMerge.Execute(new string[] {"ProductLetterheadHeader"}, new string[] {"C:\\Temp\\img.jpg"});

doc.MailMerge.ExecuteWithRegions(GetTableData());

doc.Save("C:\\Temp\\out.docx");

private static DataSet GetTableData()

{

DataSet ds = new DataSet();

// Create two tables and add them into the data set.

DataTable parent = new DataTable("Base");

parent.Columns.Add("id");

parent.Columns.Add("EntityLegalNameAddress");

parent.Columns.Add("AccountNumber");

parent.Columns.Add("InvestmentSecurityCode");

parent.Columns.Add("ToDate");

parent.Columns.Add("EntityTFNABNQuotedNotQuoted");

DataTable child = new DataTable("AccountTransactionWithinPerio");

child.Columns.Add("parnetId"); // this column is needed to create relationshoip between tables.

child.Columns.Add("TxnDate");

child.Columns.Add("Type");

child.Columns.Add("RegRef");

child.Columns.Add("NetOn");

child.Columns.Add("NetOff");

child.Columns.Add("UnitsBalance");

ds.Tables.Add(parent);

ds.Tables.Add(child);

ds.Relations.Add(parent.Columns["id"], child.Columns["parnetId"]);

// Structure of our datasource is ready. Let's add some dummy data.

Random rnd = new Random();

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

{

parent.Rows.Add(new object[] { i,

string.Format("EntityLegalNameAddress #{0}", i),

string.Format("AccountNumber #{0}", i),

string.Format("InvestmentSecurityCode #{0}", i),

string.Format("ToDate #{0}", i),

string.Format("EntityTFNABNQuotedNotQuoted #{0}", i)});

for (int j = 0; j < rnd.Next(1, 15); j++)

{

child.Rows.Add(new object[] { i,

string.Format("TxnDate {0} {1}", i, j),

string.Format("Type {0} {1}", i, j),

string.Format("RegRef {0} {1}", i, j),

string.Format("NetOn {0} {1}", i, j),

string.Format("NetOff {0} {1}", i, j),

string.Format("UnitsBalance {0} {1}", i, j)});

}

}

return ds;

}

I have sent the input and the output documents produced on my side to your e-mail.

Best regards,

Thanks Andrey

I am very impressed by the great support you and Aspose provide.

Regarding this solution. I believe I understand how it works and it would work perfectly for most situations. I suspect there is a limitation though and I wanted to check with you before changing my code.

With your approach there would always be the same logo in the header for all records in the Base table, wouldn't it? If in for instance there were different logos for different investment funds (as is the case) for this template. Then your code will still only ever put the logo for one of them in all headers. Is this correct?

I am guessing that your row:

doc.MailMerge.Execute(newstring[] {"ProductLetterheadHeader"},new string[] {"C:\\Temp\\img.jpg"});

changes the image in the header in the template before the mail merge happens and therefore the same logo will appear on all records regardless if my table data suggests otherwise.

If this is the case then I feel that this is a slight limitation to the product and I would suggest that you add to the wish-list that it would be possible to put a TableStart in the header and its' corresponding TableEnd in the footer. That would in my opinion add serious value to the solution. I can imagine that this is something all users would benefit from. If this is unreasonably hard to achieve or not is something I cannot assess.

Regards,

!Rob

Hello

Thank you for additional infornation. I think the best way to achive what you need is generating the above reports by performing mail merge several times and building up a resulting document from several documents or fragments. The Product Catalog and Dinner Invitation samples in the demo project supplied with Aspose.Words demonstrate the suggested techniques.
http://www.aspose.com/demos/.net-components/aspose.words/csharp/general/product-catalog.aspx

http://www.aspose.com/demos/.net-components/aspose.words/csharp/general/dinner-invitation.aspx

Best regards,