Hi,
I’ve noticed that at some version between 24.2 and current version MailMerge.Execute() started updating fields that are not used in the mailmerge - in attached example this is happening for a FormField. Is it possible to stop this new behavior?
FormFieldDefaultValue.zip (23.6 KB)
@acturisaspose
It seems that you are experiencing an issue where the MailMerge.Execute()
method is updating fields that are not part of the mail merge, specifically a FormField. This behavior change may have been introduced in a recent version of the library.
To address this, you can utilize the MailMerge
options to control how fields are processed during the merge. Specifically, you can set the UseNonMergeFields
property to false
, which will prevent the mail merge from updating fields that are not explicitly defined in your data source.
Here’s a code snippet demonstrating how to set this property:
Document doc = new Document("path/to/your/document.docx");
doc.getMailMerge().setUseNonMergeFields(false); // Prevent updating non-merge fields
String[] fieldNames = { "FieldName1", "FieldName2" };
Object[] fieldValues = { "Value1", "Value2" };
doc.getMailMerge().execute(fieldNames, fieldValues);
doc.save("path/to/save/merged/document.docx");
By setting UseNonMergeFields
to false
, the mail merge process will only update the fields that are part of the merge, thus avoiding any unintended updates to other fields like FormFields.
If you continue to experience issues, consider checking the release notes or documentation for any additional changes related to mail merge behavior in the versions you are using.
Feel free to reach out if you have further questions or need additional assistance!
Sources:
[1]: MailMergePlugin.cs
[2]: WorkingWithFields.java
Also please note that in the example I am already turning off the inclusion of non-mailmerge fields:
doc.MailMerge.UseNonMergeFields = false;
@acturisaspose MailMerge.UseNonMergeFields
does not disable updating of other fields. This property specifies that in addition to MERGEFIELD
fields, mail merge is performed into some other types of fields and also into {{fieldName}}
tags.
Aspose.Words updates all field upon executing mail merge, this is an expected behavior. There is no way to turn this off. But you can disable updating particular field in your document. For example, the below example demonstrates how to disable updating of input text form fields:
Document doc = new Document(@"C:\Temp\in.docx");
foreach (Field f in doc.Range.Fields)
{
if(f.Type == FieldType.FieldFormTextInput)
f.IsLocked = true;
}
string[] fieldNames = { "FieldName1", "FieldName2" };
object[] fieldValues = { "Value1", "Value2" };
doc.MailMerge.Execute(fieldNames, fieldValues);
doc.Save(@"C:\Temp\out.docx");
Hi Alexey,
The approach you suggested works perfectly, thanks!
1 Like