Repacing mailmergefield by another documents


Hello, it is possible using aspose.word to insert documents in a document by replacing mailmergefieds by documents.
for exemple if I’ve.


I’d like to replace <> by documents .doc.

It is possible for me to store document filemane on a database table like I’ve done with Image using aspose.word.



Here are the steps to insert documents into merge fields in another document:

1. Create a document and insert merge fields into it.

2. Document.MailMerge execute will work fine as is for simple string fields.

3. Define an event handler for the MergeField event.

4. Inside your event handler, you need to load another Document. Once the document is loaded, you need to copy or move content from the resume document to your report document.

5. Moving/copying document is done section by section. See Section object methods. Also, see here:
Please note that some formatting (especially list formatting) might be lost when moving content from one document to another.

Refer to this thread to get more info:

Also, search the forum, there are many threads dedicated to this problem.


Thanks. I must add that copying lists is now handled correctly and list formatting is not lost.


Hello, by inserting this document (see attachement ) to another I’ve error :
Enable to read beyong the end of the stream.



private void HandleMergeFieldEvent(object sender, MergeFieldEventArgs e)
//This creates a new document builder for every event.
//Although DocumentBuilder is a lightweight object, it still
//might be better to cache it in a field of this class.
DocumentBuilder builder = new DocumentBuilder(e.Document);

if (e.FieldName == "Description")
string fieldValue = e.FieldValue.ToString();

if (fieldValue.IndexOf("Blue") != -1)
builder.Font.Color = System.Drawing.Color.Blue;
builder.Font.Color = System.Drawing.Color.Red;

//Write the text using the builder, don't let the mail merge engine to insert text too.
e.Text = "";


Try another code to combine documents: