Free Support Forum -

Mail Merge Field question

Mail merge fields like Image:MyFieldName, TableStart:MyTable, TableEnd:MyTable

Are these fields and syntax specific to aspose or these are from microsoft word? Becuase when i try to create template using MS word and add Merge Field, i dont get
Image or table option.

Hi there,

Thanks for your inquiry.

This types of merge fields contain special Aspose.Words syntax which does not exist in Microsoft Word. Therefore there are no options for them found inside MS Word. Instead you write the prefix e.g “Image:MyField” along with the field name.

For a demonstration of how to do this, you can check out the video here.


Thank You that video really helped

The example here says in order to handle Image Field we need to implement class from IFieldMergingCallback and then set Document.MailMerge.FieldMergingCallback.
Why in the example above they are setting stream if the field value is already byte[]?

For me its working without implementing the IFieldMergingCallback class. How? Here is my code

public void Word_MailMerge_And_Add_Signature_From_DB()
            Aspose.Words.Document document = null;
            using (Stream wordSourceStream = new MemoryStream())
                DBOperations dbOperations = new DBOperations();
                dbOperations.FetchFile_Using_SqlFileStream(wordSourceStream, "Cover Letter with mail merge fields.docx");
                wordSourceStream.Seek(0L, SeekOrigin.Begin);
                document = new Aspose.Words.Document(wordSourceStream);
        <SPAN style="COLOR: green">//Perform Mail Merge</SPAN>
        document.MailMerge.Execute(<SPAN style="COLOR: #2b91af">ContextData</SPAN>.GetDataTable());
        document.Save(<SPAN style="COLOR: #a31515">"output.docx"</SPAN>);
    }</PRE><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px"><SPAN style="COLOR: blue">internal</SPAN> <SPAN style="COLOR: blue">class</SPAN> <SPAN style="COLOR: #2b91af">ContextData</SPAN>
    <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: #2b91af">DataTable</SPAN> GetDataTable()
        <SPAN style="COLOR: #2b91af">DataTable</SPAN> table = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">DataTable</SPAN>();
        table.Columns.Add(<SPAN style="COLOR: #a31515">"YourName"</SPAN>);
        table.Columns.Add(<SPAN style="COLOR: #a31515">"YourAddress"</SPAN>);
        table.Columns.Add(<SPAN style="COLOR: #a31515">"YourCityStateZip"</SPAN>);
        table.Columns.Add(<SPAN style="COLOR: #a31515">"ClientName"</SPAN>);
        table.Columns.Add(<SPAN style="COLOR: #a31515">"Signature"</SPAN>,<SPAN style="COLOR: blue">typeof</SPAN>(<SPAN style="COLOR: #2b91af">Array</SPAN>));

        <SPAN style="COLOR: #2b91af">DataRow</SPAN> row = table.NewRow();
        row[<SPAN style="COLOR: #a31515">"YourName"</SPAN>] = <SPAN style="COLOR: #a31515">"Jhon Dow"</SPAN>;
        row[<SPAN style="COLOR: #a31515">"YourAddress"</SPAN>] = <SPAN style="COLOR: #a31515">"4500 Test Avn"</SPAN>;
        row[<SPAN style="COLOR: #a31515">"YourCityStateZip"</SPAN>] = <SPAN style="COLOR: #a31515">"Austin, TX 75365"</SPAN>;
        row[<SPAN style="COLOR: #a31515">"ClientName"</SPAN>] = <SPAN style="COLOR: #a31515">"Some Client"</SPAN>;
        row[<SPAN style="COLOR: #a31515">"Signature"</SPAN>] = GetImage();
        <SPAN style="COLOR: blue">return</SPAN> table;

    <SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">byte</SPAN>[] GetImage()
        <SPAN style="COLOR: #2b91af">DBOperations</SPAN> dbOperations = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">DBOperations</SPAN>();
        <SPAN style="COLOR: #2b91af">MemoryStream</SPAN> imageStream = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">MemoryStream</SPAN>();
        dbOperations.FetchFile_Using_SqlFileStream(imageStream, <SPAN style="COLOR: #a31515">"signature_sample.gif"</SPAN>);
        imageStream.Seek(0L, <SPAN style="COLOR: #2b91af">SeekOrigin</SPAN>.Begin);
        <SPAN style="COLOR: blue">return</SPAN> imageStream.ToArray();


Thanks for your request. In the article we just show a way how you can load images from any source you like. The code provided there just demonstrates the technique. If you datasource already contains data of images as byte array you do not need the IFieldmergingCallback.

Best regards,