Free Support Forum - aspose.com

Removing field referencing bookmark when MailMergeCleanupOptions.RemoveUnusedFields set on MailMerge

Hi,

I have got an issue with cleaning unused fields.

In the template attached FooterField.zip (19.1 KB) we have a merge field {MERGEFIELD Contact_FullName} which does not have a corresponding merge data in data source, so we want this field to be removed after mail merge. This is why we have set MailMergeCleanupOptions.RemoveUnusedFields on MailMerge object.

On the other hand we have a Field of type Ref in footer which resolves to bookmark: {REF DOCUMENT_TITLE_NEW*CHARFORMAT} which we want to resolve to bookmark existing in the template. With the MailMergeCleanupOptions.RemoveUnusedFields option being set, the field is treated as unused and removed. When the cleanup option is not set - the field is resolved correctly.

Code:

        static void Main(string[] args)
        {
                var dataFile =
                new XDocument(new XElement("Data", new XElement("PrimaryRecordSet", new XAttribute("ID", "Main"),
                    new XElement("MergeField1", "mergefield1text"))));

        var mergeData = createDummyMergeData(dataFile);

        var document = new Document(@"FooterField.dotx");

        document.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields;

        document.MailMerge.Execute(mergeData);
        document.MailMerge.ExecuteWithRegions(mergeData);

        document.UpdateFields();

        document.Save("OutputFiles/Output.docx");
        }

    private static MergeData createDummyMergeData(XDocument dataFile)
    {
        var primaryRecordSet = new PrimaryRecordSet("Main", new Dictionary<string, RecordSetElement>() { { "Date", new RecordSetElement("20160113", false, false) } });
        var secondaryRecordSets = new List<SecondaryRecordSet>();
        var mergeData2 = new MergeData(primaryRecordSet, secondaryRecordSets);

        var dataElement = dataFile.Root;

        var PRSElement = dataElement.Elements().FirstOrDefault(e => string.Equals(e.Name.LocalName, "PrimaryRecordSet"));

        var childElements = PRSElement.Elements().ToList();
        var elements = childElements.ToDictionary(child => child.Name.ToString(),
            child => new RecordSetElement(child.Value.Trim(), false, false));
        var primaryRecordSet2 = new PrimaryRecordSet("Main", elements);

        return new MergeData(primaryRecordSet2, secondaryRecordSets);
    }

Could you please take a look and advise how to set the cleaup options to clean mergefields that not resolved with merge data and to preserve fields referencing bookmarks?

Thanks,
Ewa

@acturisaspose,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-17044. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

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