MailMerge and images

Hi,

We have been using the Aspose.Word mailmerge feature to insert images into documents. However, we have recently upgraded Aspose.Word to version 2.1.9.0 from 1.6.3.0 and now the image mailmerge no longer works. Any ideas?

Simple C# Console Sample.

using System;

namespace AsposeWordTest
{
    using Aspose.Word;

    class Test
    {
        [STAThread]
        static void Main(string[] args)
        {
            Aspose.Word.Document
            src = new Document(@"c:\asposetest.doc");

            src.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField);

            string[] fields = new string[5];
            string[] values = new string[5];

            fields[0] = "Hello1"; values[0] = "Hello1";
            fields[1] = "Hello2"; values[1] = "Hello2";
            fields[2] = "Hello3"; values[2] = "Hello3";
            fields[3] = "Hello4"; values[3] = "Hello4";
            fields[4] = "Hello5"; values[4] = "Hello5";

            src.MailMerge.Execute(fields, values);

            src.Save(@"c:\aspose.doc", SaveFormat.FormatDocument);
            src = null;
        }

        private static void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Image field detected " + e.FieldName + " val " + e.FieldValue);
        }
    }
}

The diagnostic message never gets printed and the debugger doesn’t get tripped for the event handler.

It works fine in our tests. Please double check that the merge field in the document is named like this “Image:MyFieldName” (press Alt+F9 to see field codes in MS Word and check then).

Yep, it is. These files have been working fine whilst we have been using 1.6 since it was released. It was only with the installation recently that they stopped working.

I’ve tried with the existing working templates, and new template generated solely for testing, and it just doesn’t get called. I can package up the dlls and source with a template if you think it might help.

Hi Ross,

I’ve got your files here and will test in the next 1-2 days.

Ok, I see the point.

A side effect of one of the recent changes we’ve made is that the merge field event is no longer fired for fields that are not found in the data source.

We realize it became inconvenient and will make sure it is called again in one of the next hotfixes.

In the meantime you can workaround if you add a field named RossTest to your list of fields you pass to mail merge.

OK that does appear to work round it. I presume I’ll have to change it when the hotfix version is available?

Many thanks.

I think this workaround will keep working okay when I change back to fire event for non existant fields.

Aspose.Word 2.1.13 is out that fires the event handler for all fields found in the document regardles of whether they were found in the data source or not, for more info see https://docs.aspose.com/words/net/mail-merge-and-reporting/ and https://reference.aspose.com/words/net/aspose.words.mailmerging/imagefieldmergingargs/.