One Unmerged Field not deleting

Hi Team,
I am using Aspose and trying to generate document using aspose fields tags.It is deleting all unmerged if condition as well but except one unmerged field.Can you please check why

@Gurpreet.kaur

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

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.

PS: To attach these resources, please zip and upload them.

If you try this document with ALT +F9 you will see the problem

@Gurpreet.kaur

Unfortunately, we have not found any document with your post. Please ZIP and attach the requested resources here for testing. Thanks for your cooperation.

Docx.zip (10.4 KB)

@Gurpreet.kaur

Please share the requested resources here for testing. We will investigate the issue and provide you more information about your query.

Hi tahir,

I have attached the document Docx.zip when no data is coming through Dataset ,It will show as unmerged even thought it tried to delete the fields still it is not hiding this one and we are sending letters to thousands of users which look so bad not to hide this one .

Public Function GenerateDocument(ByVal xmlTemplateToConvertInfo As XmlTemplateToConvertInfo) As String Implements IDocumentConverterBO.GenerateDocument
Dim doc As Document
Dim generatedDocumentFileName As String = String.Empty

    If xmlTemplateToConvertInfo Is Nothing Then Throw New ArgumentNullException("xmlTemplateToConvertInfo")
    Try
        If File.Exists(xmlTemplateToConvertInfo.TemplateFileNamePath) Then
            doc = New Document(xmlTemplateToConvertInfo.TemplateFileNamePath)
            If doc.MailMerge.GetFieldNames().Length = 0 Then Throw New Exception("Old 2003 document Xml" + xmlTemplateToConvertInfo.OutputFileName)
            'This handles the embedded images as a callout to the MailMergeInsertImage class (defined below)
            doc.MailMerge.FieldMergingCallback = New MailMergeInsertImage(xmlTemplateToConvertInfo.DocumentData)
            ' doc.MailMerge.CleanupOptions = Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveUnusedRegions
            'First pass passes each table to the mail merge one at a time. This is so that
            'we don't have to setup table regions in the document. As long as all table fields are uniquely 
            'named, this approach will work. This is exactly how the legacy CEGES document generator worked
            'which is what the current datasets were designed to target.
            If Not xmlTemplateToConvertInfo.DocumentData Is Nothing AndAlso xmlTemplateToConvertInfo.DocumentData.Tables.Count > 0 Then
                For Each Table As DataTable In xmlTemplateToConvertInfo.DocumentData.Tables
                    If Table.Rows.Count = 1 Then
                        If Not (Table.Rows(0).RowState.ToString() = DataRowState.Deleted.ToString()) Then
                            doc.MailMerge.Execute(Table)
                        End If
                    End If
                Next
                doc.MailMerge.CleanupOptions = Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveUnusedRegions
                'this final pass handles the repeating table scenario
                doc.MailMerge.ExecuteWithRegions(xmlTemplateToConvertInfo.DocumentData)
                doc.MailMerge.UseNonMergeFields = True
                doc.MailMerge.DeleteFields()
                'Saves the document to a stream using the specified format
                generatedDocumentFileName = SaveMergeTemplate(doc, xmlTemplateToConvertInfo)
            End If

        End If
    Catch ex As Exception
        Throw New Exception("DocumentConverterBO.GenerateDocument Exception" + ex.Message + xmlTemplateToConvertInfo.TemplateFileNamePath, ex.InnerException)
    Finally
        doc = Nothing
    End Try
    Return generatedDocumentFileName
End Function

mports Aspose.Words.Reporting
Imports Aspose.Words
Imports System.IO
Imports Aspose.Cells
Imports Aspose.Words.Tables

Public Class MailMergeInsertImage
Implements IFieldMergingCallback

Public data As DataSet
Public Sub New(ds As DataSet)
    data = ds
End Sub

Public Sub FieldMerging(e As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging
    'Dim builder As DocumentBuilder
    'builder = New DocumentBuilder(e.Document)
    'If e.FieldValue Is Nothing Then
    '    Dim row As Row = e.Field.Start.GetAncestor(NodeType.Row)
    '    If row IsNot Nothing Then
    '        e.Text = ""
    '        'row.Remove()
    '    End If
    ' Dim cell As Cell = e.Field.Start.GetAncestor(NodeType.Cell)
    '  builder.MoveToMergeField(e.FieldName)
    ' Cell.Remove()

    ' End If
End Sub

Public Sub ImageFieldMerging(e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging


    Dim table As DataTable = data.Tables(e.TableName)
   ' Try
        If e.FieldValue IsNot Nothing Then
            If table.Columns.Contains(e.FieldName) Then
                If table IsNot Nothing And table.Rows.Count > 0 Then
                    'Dim imageBytes As Byte() = Convert.FromBase64String(table.Rows(0)(e.FieldName).ToString())
                    e.ImageStream = New MemoryStream(CType(table.Rows(0)(e.FieldName), Byte()))

                End If
            End If
        End If

    'Finally
    '    If (Not (e.ImageStream) Is Nothing) Then
    '        e.ImageStream.Close()
    '    End If

    'End Try
End Sub

End Class

@Gurpreet.kaur

In your case, we suggest you please add RemoveContainingFields in MailMerge.CleanupOptions to get the desired output. Please read the following article.

How to Remove Unmerged Fields, Empty Paragraphs and Unmerged Regions

We tried That one as well RemoveContainingFields in MailMerge.CleanupOptions but it is not removing this particular one

@Gurpreet.kaur

Please share the requested resources here for testing. If you cannot supply us with this information, we will not be able to investigate your issue and raise a ticket. Thanks for your cooperation.

Docx.zip (10.4 KB)
I sent you the code Which i was using and here is the Zip file the end result comes out and showing only one tag which we want to hide .

@Gurpreet.kaur

You have shared a document that only contains the IF fields. Your issue is related to removing these fields from the document while performing mail merge. You can try the following cleanup options. Hope this helps you.

doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields Or MailMergeCleanupOptions.RemoveContainingFields Or MailMergeCleanupOptions.RemoveEmptyParagraphs Or MailMergeCleanupOptions.RemoveUnusedRegions

If you still face problem, please ZIP and attach your input document and code example without compilation errors. We need these resources for testing purpose. After investigation, we can share the problem of this issue and will share correct solution with you. So, please share the requested resources for investigation.

Aspose-Issue.zip (22.5 KB)

Hi Tahir,

I have attached the code,Input Document and Result Document,It will delete every other unmerged filed but not this «EmployerAddressLine4» ,I tried the about mentioned suggestion but Its not deleting this one .

@Gurpreet.kaur

You have shared the same code example that contains the compilation errors. We have created following simplified example and tested the scenario. We have not found the shared issue while using the latest version of Aspose.Words for .NET 19.10. So, please use Aspose.Words for .NET 19.10.

Dim table As DataTable = New DataTable("root")
    table.Columns.Add("EmployerName", GetType(String))
    table.Columns.Add("EmployerAddressLine1", GetType(String))
    table.Columns.Add("EmployerAddressLine2", GetType(String))
    table.Columns.Add("EmployerAddressLine3", GetType(String))
    table.Columns.Add("EmployerAddressLine4", GetType(String))
    table.Columns.Add("EmployerAddressCity", GetType(String))
    table.Columns.Add("EmployerAddressProvince", GetType(String))
    table.Columns.Add("EmployerAddressCountry", GetType(String))
    table.Columns.Add("EmployerAddressPostalCode", GetType(String))
    Dim newRow = table.NewRow()
    newRow("EmployerName") = ""
    newRow("EmployerAddressLine1") = ""
    newRow("EmployerAddressLine2") = ""
    newRow("EmployerAddressLine3") = ""
    newRow("EmployerAddressLine4") = ""
    newRow("EmployerAddressCity") = ""
    newRow("EmployerAddressProvince") = ""
    newRow("EmployerAddressCountry") = ""
    newRow("EmployerAddressPostalCode") = ""
    table.Rows.Add(newRow)
    Dim doc As Document = New Document(MyDir & "Input.xml")
    doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields Or MailMergeCleanupOptions.RemoveEmptyParagraphs
    doc.MailMerge.Execute(table.Rows(0))
    doc.Save(MyDir & "19.10.docx")

Hi Tahir,

I am using Aspose 14,Problem is with footer as these employer values are in footer and in if condition .
Any thoughts on that

I upgraded to ASPOSE 19.10 ,Problem is with footer as these employer values are in footer and problem IS ONLY WITH if CONDITION

@Gurpreet.kaur

We tested the scenario using your input document and simplified code example shared in my previous post. We have not found the shared issue. You may execute the same example at your end to check this case.

We request you please share the simplified code example that you are using to reproduce this issue at our end. The code snippet you shared in this thread is incomplete and contains compile time errors.