MailMerge.Execute vs MailMerge.ExecuteWithRegions

Hi there.

We’ve got a solution whereby our users can build Word documents with mail merge fields - both individually and in repeating sections (tablestart etc in Word).

We’ve got a custom datasource that represents the data as it’s held in our system and at the moment we’re having to call both MailMerge.ExecuteWithRegions, followed by MailMerge.Execute. This is because we don’t know in advance whether the document that the user has supplied contains a region or not.

Please find attached a CSharp example where we’ve simplified it down to the bare minimum and created an example nested IMailMergeDataSource. The example produces a series of different files based on combinations of calling Execute and ExecuteWithRegions and with templates that have and don’t have regions (the templates are also created in code to make reproduction easier).

Would it be possible to simply the interface in Aspose so that there is a single call that either identifies which to do or executes both?

cheers,

g

Hi Gary,

Thanks for your inquiry and sorry for the delayed response. I am afraid there exists no such method that would be helpful in applying the appropriate mail merge procedure (e.g. ExecuteWithRegions or Execute) just by analyzing the template documents itself. But, I think, you can easily identify the existence of any regions in your template document by using the following code snippet:

ArrayList regionNames = new ArrayList();
string[] fieldNames = doc.MailMerge.GetFieldNames();
foreach(string name in fieldNames)
{
    if (name.StartsWith("TableStart:") || name.StartsWith("TableEnd:"))
    {
        string regionName = name.Split(new char[]
        {
            ': '
        })[1];
        if (!regionNames.Contains(regionName))
            regionNames.Add(regionName);
    }
}

I hope, this will help.

Best Regards,

Hi Gary,

Thanks for your inquiry.

In addition to Awais’s reply, perhaps you could find some useful information by studying the code here: https://forum.aspose.com/t/60565

I agree with Awais in that Execute and ExecuteWithRegions need to be separate as they do very different things. Therefore we cannot combine the functionality in any way.

Thanks,