We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

MailMerge.Execute does not update the IF field using .NET

We’re getting an issue mail merging with templates which have a merge field in the condition where the merge field is not surrounded by quotes and the data being merged has spaces and numbers.

For example
{ IF { MERGEFIELD numbers } = “Something 1” “TRUE” “FALSE” }

If the numbers mergefield is merged as “Something 1”, aspose merges the IF as “FALSE” whereas MS Word merges the field as “TRUE”

If there aren’t both numbers and spaces in the data, or if the mergefield is wrapped in double quotes then the IF is correctly merged as “TRUE” by aspose.

We are using Aspose.Words for .NET version 19.10.0

I have uploaded a console app demonstrating the issue.
AsposeSimpleMerge.zip (42.4 KB)

@daniel.roper

Please call Document.UpdateFields method before saving document as shown below to get the desired output.

Document document = new Document(MyDir + "template.docx");

document.MailMerge.TrimWhitespaces = true;
document.MailMerge.MergeDuplicateRegions = true;
document.MailMerge.CleanupOptions = MailMergeCleanupOptions.None;

document.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields
                            | MailMergeCleanupOptions.RemoveEmptyParagraphs
                            | MailMergeCleanupOptions.RemoveContainingFields;
document.MailMerge.Execute(set.Tables[0]);
document.UpdateFields();
document.Save(MyDir + "19.10.docx");

Thanks for your reply, I’ve tried adding the line as suggested to the console app as suggested, but unfortunately I’m still seeing the same output.

I’ve found that after the execute, document.Range.Fields doesn’t have any fields left so UpdateFields seems like it would be too late.

Also, according to the UpdateFields documentation
“There is no need to update fields after executing a mail merge because mail merge is a kind of field update and automatically updates all fields in the document.” https://apireference.aspose.com/net/words/aspose.words/document/methods/updatefields

I’ve uploaded a new version of the console app with the line added.
AsposeSimpleMergeWUpdateFields.zip (23.5 KB)

@daniel.roper

Please call Document.UpdateFields and Document.UnlinkFields methods as shown below and remove MailMergeCleanupOptions.RemoveContainingFields from CleanupOptions. Hope this helps you.

public static Stream CreateDocument(DataSet data, Stream template, MailMergeMainDocumentType documentType)
{
    Document document;
    try
    {
        document = new Document(template);
    }
    catch (IncorrectPasswordException)
    {
        return null;
    }

    document.MailMerge.TrimWhitespaces = true;
    document.MailMerge.MergeDuplicateRegions = true;
    document.MailMerge.CleanupOptions = MailMergeCleanupOptions.None;
    document.MailMergeSettings.MainDocumentType = documentType;

    document.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields
                                        | MailMergeCleanupOptions.RemoveEmptyParagraphs;
                                        //| MailMergeCleanupOptions.RemoveContainingFields;
    document.MailMerge.Execute(data.Tables[0]);

    document.UpdateFields();

    document.UnlinkFields();
    MemoryStream documentStream = new MemoryStream();
    document.Save(documentStream, SaveFormat.Docx);
    return documentStream;
}

Thanks, this has worked around the issue.

Will you consider the original issue a bug, given that the merge doesn’t seem to work correctly using the original settings?

@daniel.roper

We have logged this problem in our issue tracking system as WORDSNET-19448. You will be notified via this forum thread once this issue is resolved. You can use the shared code example as a workaround of this issue.

We apologize for your inconvenience.

@daniel.roper

Thanks for your patience. This issue has been resolved in the latest version of Aspose.Words for .NET 20.3. So, please use Aspose.Words for .NET 20.3.

The issues you have found earlier (filed as WORDSNET-19448) have been fixed in this Aspose.Words for .NET 20.4 update and this Aspose.Words for Java 20.4 update.