Free Support Forum - aspose.com

Multiple Mail Merge

Hello,

i need to perform MailMerge for multiple recipients.

I neet to get just one .doc file with one page for each recipient, like the attached document.

Is it possible to do this without using .xml and .xsd files?

Sorry for my english.

Regards,

TKnapp

Hi

Thanks for your request. I think, you can use the same approach as suggested here:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-produce-multiple-documents-during-mail-merge.html

Hope this helps. Please let us know if you need more assistance, we are always glad to help you.

Best regards,

Hello

Thank you alexey for your fast response. This how-to helped me a lot.

Unfortunately this creates a new document for each 'customer' but i neet to get just one document for all 'customers'. I tried with Document.AppendDocument but this writes the tree 'cusomers' on top of each other, it fills the same fields tree times with different texts. I attached a sample document witch shows what i attempt to do. You see, each 'customer' has a single page in the document. Is this possible?

Best regards,

TKnapp

Hi

Thank you for additional information. Sure it is possible. In this case, you should simple mail merge:

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

Also, I suppose the following article could be useful for you:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-control-new-pages-during-mail-merge.html

Best regards,

Hello

Thank you for your rapid answer.

Finallay I succeeded in merging several rows from a database table into one single document. I had several problems with blank pages at the end of the document or annoying page breaks, but now it runs quiet perfect.

[C#]

public static void ProduceMultipleDocuments(string dataDir, string srcDoc)
{
Document doc;
Document docOriginal = new Document(dataDir + srcDoc); 
DocumentBuilder builder;

// Open the database connection.
string connString = @“Provider=MySQL Provider; Data Source=localhost; User ID =user; Password=pswrd; Initial Catalog=aspose;”;
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
try
{
// Get data from a database.
OleDbCommand cmd = new OleDbCommand(“SELECT * FROM multmailmerge”, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable data = new DataTable();
da.Fill(data);
// Open the template document.
doc = new Document();
doc.RemoveAllChildren();
int counter = 1;
// Loop though all records in the data source.
foreach (DataRow row in data.Rows)
{
if (counter == 1)
{
// Clone the template instead of loading it from disk (for speed).
Document dstDoc = (Document)docOriginal.Clone(true);
dstDoc.MailMerge.Execute(row);
doc = (Document)dstDoc.Clone(true);
builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
builder.Writeln(“counter == 1”);
builder.InsertBreak(BreakType.SectionBreakNewPage);
doc.LastChild.Remove();
}
else
{
// Clone the template instead of loading it from disk (for speed).
Document dstDoc = (Document)docOriginal.Clone(true);
dstDoc.MailMerge.Execute(row);

builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.SectionBreakNewPage);
doc.AppendDocument(dstDoc, ImportFormatMode.KeepSourceFormatting);
}
counter++;
}
doc.Save(“C:\TestFile Out.doc”);
}
finally
{
// Close the database.
conn.Close();
}
}

Regards,
   TKnapp

Hi

It is perfect that you managed to achieve what you need. Please let me know if you need more assistance, I will be glad to help you.

Best regards,