Delete Unused Merge Fields (but not Hyperlinks) in DOCX Document after Mail Merging using C# .NET API Code


I have requirement to remove the merge fields (but not hyperlinks) post-merging.
Merge fields can be shown when clicking Alt+F9, see here :
MergeField.gif (11.0 KB)

and this is my code:

                var doc = new Document(ms);
                doc.MailMerge.Execute(fields.ToArray(), data.ToArray());
                // should do something here to remove the merge fields
                doc.Save(generatedDoc, SaveFormat.Docx);

I don’t know how to do that, can someone help please ?


Please ZIP and upload your simplified input Word document (from where you want to remove merge fields but not the hyperlinks) and your expected Word document (DOCX file) showing the desired output here for testing. Please create expected document by using MS Word. We will then investigate the scenario on our end and provide you more information.

Hi @awais.hafeez (64.9 KB)

Please find attached the Zip file that contains:
• Sample.doc: This is the As-Is file, click Alt + F9 to see the merge fields and the code behind
• Sample - Target.docx: This is the To-Be file, if you click Alt + F9, the user can’t see the merge fields and the code behind

In short, the files are being generated from templates by one of our internal applications. The legacy solution was using VBA to remove the merge fields and the code behind.
The requirement is to remove the merge fields and the code behind using C#/Aspose code. As shown in the code above, we are using MailMerge.


Thanks for the additional information. Please also use your internal application to perform Mail Merge operation on “Sample.doc” and share the ‘Aspose.Words for .NET’ generated output DOCX file showing the undesired behavior here for further testing.

It would be great if you please also create a standalone simple console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for further testing. Please do not include Aspose.Words DLL files in it to reduce the ZIP file size. Thanks for your cooperation.

Hi @awais.hafeez

I’ve created a more simplified sample that contains only one expression, which uses a merge field, and a link. (37.9 KB)

Input.doc was generated from our internal application while Output.docx was processed/created manually to show the desired behaviour.

Basically, what are we trying to achieve is replacing the expressions including the merge fields with the values only but the links should not be affected.

Unfortunately, I’m not in a position I can write console application as I’m not a .net developer and I’m just modifying an existing code, so hopefully the new sample would be helpful.

However, as shown in my first post we are passing on the values to the templates via doc.MailMerge.Execute(fields.ToArray(), data.ToArray()); to generate documents like Input.doc – the template that Input.doc was created from is also attached.

I’d like to remind you that to view the expressions (and merge fields) in Input.doc click ALT+F9 and if you do the same in Output.docx you won’t be able to see the expressions as the file contains values only.

Thanks in advance


C# .NET Code to Convert Fields in Word Document to Static Text

Document doc = new Document("E:\\Temp\\SampleMergeField\\Input.doc");

// After executing the mail merge, please try adding the following foreach loop in your code:
foreach(Field field in doc.Range.Fields)
    if (field.Type == Aspose.Words.Fields.FieldType.FieldIf)


Thanks @awais.hafeez that was very helpful