Remove Unused Merge Fields from Word Document after Mail Merge & Retain Table Layout C# .NET

We have forms that are Word versions of official government forms in PDF format. We have inserted merge fields into these forms and have been using Aspose.Words version 4.2.0 to merge our customers’ data into these forms.

We have not had any problems until recently when some of our customers reported the issue that “a table in this document has become corrupted” when opening a document after a merge operation.

After investigation, we have determined that this problem arises after the customer’s Microsoft Office is updated to version 1903.

I have tested with the latest Aspose.Words version (5.2.0) to see if it would resolve the issue. It seems it might, at least it does with a simple document. However, as our license has long been expired, the limitations with the evaluation version makes it hard to ascertain that it will handle more complex documents correctly.

In one of our documents, the offending table is located on page 9. Unfortunately the evaluation version truncates page 3 and above. So what I did is copy the contents of page 9 into a separate document and test that. I’m seeing the following issues:

  1. The merge fields are processed correctly but if a field is missing in the data source, the field name is preserved in the resulting document; it should have been removed. For example, if I have in the template document a field named “A1” and the recordset (we use the ExecuteADO method) does not contain an “A1” key, the document will show “A1”. If however I create a document from scratch, insert a table with merge fields in each cell, any merge field with no value supplied will be removed.

  2. In my test document I have table A followed by table B, but in the resulting document table B will appear before table A. Maybe this is caused by the warnings that the evaluation version inserts in the document, but I’d like to be sure.

We certainly would be willing to upgrade to fix the 1903 issue, but we would like to be certain that it won’t create more problems.

@myrica,

If you want to test the latest version of Aspose.Words for .NET i.e. 19.5 without the evaluation version limitations, then you can request a 30-day Temporary License. Please refer to How to get a Temporary License .

In case the problem still remains, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words 19.5 generated output document showing the undesired behavior
  • Your expected document showing the correct output. You can create expected document by using MS Word
  • Please also create a simplified standalone console application (source code without compilation errors) that helps us to reproduce your current problem with 19.5 version on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

Thank you very much for your prompt response.

Yes, sorry, I meant version 19.5.0. No idea where I got this 5.2.0 from.

I requested a temporary license and tested again with some of our documents. As far as I can tell, it does fix the issue experienced by our customers. And the second issue I mentioned in my original post has gone away.

The first one - missing fields not removed - is still there though. I have determined that it only occurs with fields that, for some reason, have a gray background (these forms were generated over a decade ago using nobody remembers which tool, and edited occasionally as needed). However, this is, I think, something that could be easily fixed on our side, either by modifying the templates or by explicitly clearing them in our implementation of the IFieldMergingCallback.FieldMerging method.

@myrica,

Thanks for the additional information. Please let us know if you have any troubles and we will be glad to look into this further for you.