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

Free Support Forum - aspose.com

MailMergeCleanupOptions.RemoveUnusedFields removes footer mergefields

Hi,

Since we use the version 14.2.0.0, we have a problem using the MailMergeCleanupOptions.

In the source document, I have a “{ MERGEFIELD B }” in the body, and another “{ MERGEFIELD B}” in the footer. If I specify MailMergeCleanupOptions.RemoveUnusedFields, there is no more “{ MERGEFIELD B }” in the footer, so I get a blank footer.

I’ve attached a sample project to illustrate the problem. The project uses in.doc as source and produces out-OK.doc (without specifying RemoveUnusedFields) and a out-NotOK.doc (that specify RemoveUnusedFields). Everything is in the “…\Data” folder.

With version 11.10 (version actually in our product), everything is OK.

Is this normal? Is there any workaround? Am I missing something!

Thanks!

Hi there,

Thanks
for your inquiry. In your case, you need to move the cursor to the
beginning of a footer in the section. I have modified the code of FieldMerging. Please check the highlighted code below.

Hope this helps you. Please let us know if you have any more queries.

Public Sub FieldMerging(ByVal args As Aspose.Words.Reporting.FieldMergingArgs) Implements Aspose.Words.Reporting.IFieldMergingCallback.FieldMerging
Dim builder As New Aspose.Words.DocumentBuilder(args.Document)
If args.Field.Start.GetAncestor(NodeType.HeaderFooter) IsNot Nothing Then
If args.FieldName.Equals("B") Then
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary)
End If
End If
builder.MoveToMergeField(args.DocumentFieldName)
If args.FieldName.Equals("A") Then
builder.Write("Merged field ""A""")
ElseIf args.FieldName.Equals("B") Then
builder.Write("Merged field ""B""")
End If
End Sub

Wow! As usual, great, quick and suitable support! Works like a charm. I’ve modified a bit to make sure we move to the good header footer type and made it extension method to DocumentBuilder type!

Public Function MoveToMergeField(builder As Aspose.Words.DocumentBuilder, args As Aspose.Words.Reporting.FieldMergingArgs) As Boolean
Dim node As Aspose.Words.Node = args.Field.Start.GetAncestor(Aspose.Words.NodeType.HeaderFooter)
If node IsNot Nothing Then
Dim hfNode As Aspose.Words.HeaderFooter = DirectCast(node, Aspose.Words.HeaderFooter)
builder.MoveToHeaderFooter(hfNode.HeaderFooterType)
End If

Return builder.MoveToMergeField(args.DocumentFieldName)
End Function 

Thank you very much!

JSr

Hi there,

Thanks
for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.